Language/Go

Go - Account + NewAccount 캡술화/ func를 이용한 초기화

bright jazz music 2021. 2. 18. 19:42

캡슐화

package accounts

//Account struct
type Account struct {
	owner   string
	balance int
}

//NewAccount creates Account
func NewAccount(owner string) *Account {
	account := Account{owner: owner, balance: 0}
	return &account //사본 말고 메모리 주소 반환
}

 

메인

 

package main

import (
	"fmt"

	"github.com/hojuncha997/learngo/accounts"
)

func main() {
	account := accounts.NewAccount("hojun")
	fmt.Println(account)
}


===

&{hojun 0} //사본이 아니라 그 객체임. 주소값으로 나오지 않은 이유는
//반환타입이 *Account 였기 때문임