ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Go

Go : struct, type, method

mokhs 2022. 3. 20. 15:20

๊ฐœ์š”

  • Go์˜ ๊ตฌ์กฐ์ฒด(struct)์™€ ํƒ€์ž…(type) ๊ทธ๋ฆฌ๊ณ  ๋ฉ”์„œ๋“œ(method)์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž 

 

๊ตฌ์กฐ์ฒด(struct)

  • go์—์„œ๋Š” c์˜ ๊ตฌ์กฐ์ฒด์™€ ์œ ์‚ฌํ•œ ํ˜•ํƒœ๋ฅผ ์ง€์›ํ•œ๋‹ค
  • ๊ตฌ์กฐ์ฒด๋ฅผ ์„ ์–ธํ•จ์œผ๋กœ์จ ์ƒˆ๋กœ์šด ์ž๋ฃŒํ˜•์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๊ณ , type-safe์—๋„ ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค
  • ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž

type user struct {
  name string
  age  int
}

func main() {
  user := user{name: "mokhs00", age: 23}

  fmt.Println(user)
}

receiver์™€ method

  • go์—์„œ๋Š” receiver๋ฅผ ์ด์šฉํ•ด ๋ฉ”์„œ๋“œ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค
  • func (t T) Foo() {}ํ˜•์‹์ด๋ฉฐ ๊ธฐ์กด ํ•จ์ˆ˜์—์„œ ์•ž์— (t *T)๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์ด๋‹ค (T๋Š” ํƒ€์ž…์ด๋‹ค)
  • T์— ๋Œ€ํ•œ receiver๊ฐ€ ์กด์žฌํ•˜๋Š” ํ•จ์ˆ˜๋Š” T๋ฅผ ์ด์šฉํ•ด ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค ex) T.Foo()
  • ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ์ฐธ์กฐ๊ฐ’์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํฌ์ธํ„ฐ๋ฅผ ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜์ž
  • ์ถ”๊ฐ€๋กœ ํ•จ์ˆ˜๋‚˜ ๊ตฌ์กฐ์ฒด์— ๋Œ€ํ•œ ์ฃผ์„์€ ํ•ด๋‹น ์ฃผ์ฒด์™€ ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ ์‹œ์ž‘ํ•ด์•ผํ•œ๋‹ค
  • ์ฃผ์„์„ ํ†ตํ•ด ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ํžŒํŠธ๋ฅผ ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค
  • ๋‹ค์Œ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž
// Account struct
type Account struct {
  owner   string
  balance int
}

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

// Deposit x amount on the account
func (account *Account) Deposit(amount int) {
  account.balance += amount
}

func main() {
  a := account.NewAccount("mokhs")

  a.Deposit(100)
}

struct String

  • ๊ตฌ์กฐ์ฒด๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ๋‚˜์˜ค๋Š” ๋ฌธ์ž๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ๋‹ค
  • ์ถœ๋ ฅ ์‹œ์— ๊ธฐ๋ณธ์ ์œผ๋กœ String ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, String ๋ฉ”์„œ๋“œ๋ฅผ receiver๋ฅผ ์ด์šฉํ•ด ์žฌ์ •์˜ํ•˜๋ฉด ์ด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค
  • ๋‹ค์Œ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž
func (account Account) String() string {
  return fmt.Sprint(account.owner, " has: ", account.balance)
}

func main() {

  a := account.NewAccount("mokhs")
  a.Deposit(100)

  // mokhs has:100
  fmt.Println(a)
}

ํƒ€์ž…(type)

  • ๊ตฌ์กฐ์ฒด๊ฐ€ ์•„๋‹Œ ํƒ€์ž…์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์ด๋Š” type-safeํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์œ„ํ•ด์„œ ์œ ์šฉํ•˜๊ณ , ๋„ค์ด๋ฐ์„ ํ†ตํ•ด ๊ฐ€๋…์„ฑ์„ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค
  • ๋˜ํ•œ ํƒ€์ž…๋„ ๊ตฌ์กฐ์ฒด์™€ ๋™์ผํ•˜๊ฒŒ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž
// dict.go
// Dictionary type
type Dictionary map[string]string

var errNotFoundElement = errors.New("not found element")

func (dictionary Dictionary) Search(word string) (string, error) {
  value, exists := dictionary[word]

  if exists {
    return value, nil
  }

  return "", errNotFoundElement
}

// main.go
func main() {
  dict := dict.Dictionary{}
  dict["a"] = "b"

  dict.Search("a")

  fmt.Println(dict)
}
๋ฐ˜์‘ํ˜•

'Go' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Golang - Rate Limit  (0) 2023.09.16
Go : ๊ณ ๋ฃจํ‹ด(goroutine), ์ฑ„๋„(channels)  (0) 2022.03.20
Go ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž  (0) 2022.03.18