๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Data Base/Go

[์‰ฝ๊ณ  ๋น ๋ฅธ Go ์‹œ์ž‘ํ•˜๊ธฐ] #2.1 Methods part One

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 11. 2.
๋ฐ˜์‘ํ˜•

[์ถœ์ฒ˜ - Nomad Coders]

 

 

 

Method๋Š” function์ด๋ž‘ ๋น„์Šทํ•˜์ง€๋งŒ ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค.

// function
func NewAccount(owner string) *Account {
	account := Account{owner: owner, balance: 0}
	return &account
}

// method
func (a Account) Balance() int {  // func ๋ฐ”๋กœ ๋’ค์— ๋ณ€์ˆ˜์™€ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ ์ ์–ด์ค€๋‹ค.
	return a.balance
}

 

 

 

Go์—์„œ๋Š” ์•„๋ž˜์˜

(a Account)

๋ถ€๋ถ„์„ receiver๋ผ๊ณ  ํ•œ๋‹ค.

 

 

 

receiver์„ ์ž‘์„ฑํ•  ๋•Œ๋Š” ๋ช‡ ๊ฐ€์ง€ ์ง€์ผœ์•ผํ•  ๊ทœ์น™์ด ์žˆ๋‹ค.

์œ„์˜ (a Account)๋ฅผ ๋”ฐ์„œ struct์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋”ฐ์„œ ์†Œ๋ฌธ์ž๋กœ ์ง€์–ด์•ผ ํ•œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€