๋ฐ์ํ
๋ถ์ : GO์์ constructor์ ๋ง๋๋ ๋ฒ
accounts/accounts.go
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
}
main.go
package main
import (
"fmt"
"github.com/serranoarevalo/learngo/accounts"
)
func main() {
account := accounts.NewAccount("nico")
fmt.Println(account)
}
&๊ฐ ๋ถ์ด์๋ ๊ฒ์ nico๊ฐ ๋ณต์ฌ๋ณธ์ด ์๋๋ผ object๋ผ๋ ๋ป์ด๋ค.
๋ฐ์ํ
'Data Base > Go' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ฝ๊ณ ๋น ๋ฅธ Go ์์ํ๊ธฐ] #2.2 Methods part Two (0) | 2021.11.02 |
---|---|
[์ฝ๊ณ ๋น ๋ฅธ Go ์์ํ๊ธฐ] #2.1 Methods part One (0) | 2021.11.02 |
[Golang] Slices (์ฌ๋ผ์ด์ค) (0) | 2021.11.01 |
[Golang] defer (0) | 2021.11.01 |
[Golang] Switch (0) | 2021.11.01 |
๋๊ธ