๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

nomad-coder12

[์‰ฝ๊ณ  ๋น ๋ฅธ Go ์‹œ์ž‘ํ•˜๊ธฐ] #2.1 Methods part One [์ถœ์ฒ˜ - 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์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋”ฐ์„œ ์†Œ๋ฌธ์ž๋กœ ์ง€์–ด์•ผ ํ•œ๋‹ค. 2021. 11. 2.
[์‰ฝ๊ณ  ๋น ๋ฅธ Go ์‹œ์ž‘ํ•˜๊ธฐ] #2.0 Account + NewAccount ๋ถ€์ œ : 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("n.. 2021. 11. 2.
[์‰ฝ๊ณ  ๋น ๋ฅธ Go ์‹œ์ž‘ํ•˜๊ธฐ] #1.11 Structs [์ถœ์ฒ˜ - Nomad Coders] struct๋Š” object์™€ ๋น„์Šทํ•˜๋ฉด์„œ map๋ณด๋‹ค๋Š” ์œ ์—ฐํ•˜๋‹ค. struct๋Š” structure(๊ตฌ์กฐ์ฒด) ๊ฐ™์€๊ฑด๋ฐ struct๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„  ๋จผ์ € ์–ด๋–ค struct์ธ์ง€ ์ •์˜ํ•ด ์ค˜์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ์ž‘์„ฑ์„ ํ•ด์•ผ ํ•œ๋‹ค. { key: value } ๋ฅผ ์ „๋ถ€ ๋ช…์‹œํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ทธ๋ฆฌ๊ณ  struct์—๋Š” constructor๊ฐ€ ์—†๋‹ค. ์šฐ๋ฆฌ ์Šค์Šค๋กœ constructor๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค. package main import "fmt" type person struct { // struct name string // structure์˜ ํ˜•ํƒœ (object) age int // structure์˜ ํ˜•ํƒœ (object) favFood []string // structure์˜ .. 2021. 10. 29.
[์‰ฝ๊ณ  ๋น ๋ฅธ Go ์‹œ์ž‘ํ•˜๊ธฐ] #1.10 Maps [์ถœ์ฒ˜ - Nomad Coders] Go์—์„œ์˜ map์€ Python์ด๋‚˜ JavaScript์˜ object์™€ ์•ฝ๊ฐ„ ๋น„์Šทํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์™„์ „ํžˆ ๋˜‘๊ฐ™์ง€๋Š” ์•Š๋‹ค. character ํ˜•ํƒœ์˜ map์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•ด์•ผ ํ•  ๊ฒƒ์€ map์˜ Key์™€ Value์˜ Type์„ ์„ ์–ธํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  map๋„ range๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ˜๋ณต๋ฌธ์— ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. package main import "fmt" func main() { nico := map[string]string{"name": "nico", "age": "12"} for key, value := range nico { fmt.Println(key, value) } } ์ด๋ ‡๊ฒŒ๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. // Key๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์ง€ ์•Š์„ ๋•Œ package main import ".. 2021. 10. 29.
[์‰ฝ๊ณ  ๋น ๋ฅธ Go ์‹œ์ž‘ํ•˜๊ธฐ] #1.9 Arrays and Slice [์ถœ์ฒ˜ - Nomad Coders] Go ์—์„œ๋Š” Array๋ฅผ ๋งŒ๋“œ๋ ค๋ฉด Array์˜ ๊ธธ์ด๋ฅผ ๋ช…์‹œํ•ด ์ค˜์•ผ ํ•œ๋‹ค. names := [5]string{"nico", "lynn", "dal"} [] ์•ˆ์— length๋ฅผ ๋ช…์‹œํ•ด์ฃผ์—ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ Slice ์ธ๋ฐ, Go ์—์„œ์˜ Slice๋Š” Array๋ž‘ ๋น„์Šทํ•˜๋‹ค. ํ•˜์ง€๋งŒ Slice๋Š” length ์—†์ด๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์›ํ•œ๋‹ค๋ฉด ์š”์†Œ๋ฅผ ๋” ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. Slice์˜ ์‚ฌ์šฉ๋ฒ• names := []string{"nico", "lynn", "dal"} ๊ทธ๋ƒฅ [] ์•ˆ์— length๋ฅผ ์„ ์–ธํ•˜์ง€ ์•Š์œผ๋ฉด ๋˜๋Š”๋“ฏ. Slice ์— item์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ append()๋ผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. append๋Š” 2๊ฐœ์˜ arguments๋ฅผ ์š”๊ตฌํ•œ๋‹ค. ์ฒซ๋ฒˆ์งธ arg.. 2021. 10. 29.
[์‰ฝ๊ณ  ๋น ๋ฅธ Go ์‹œ์ž‘ํ•˜๊ธฐ] #1.8 Pointers! [์ถœ์ฒ˜ - Nomad Coders] a์˜ ๊ฐ’์„ ๋ฐ”๊ฟ”๋„ b์˜ ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค! package main import "fmt" func main() { a := 2 b := a a = 10 fmt.Println(a, b) } package main import "fmt" func main() { a := 2 b := a a = 10 a = 200202020202020200 fmt.Println(a, b) } ๋ฌด์—‡์ธ๊ฐ€์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๋ณด๊ณ  ์‹ถ์„๋• '&'๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. & = address // ๋ฌด์—‡์ธ๊ฐ€์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ณด๋Š” ๋ฐฉ๋ฒ• package main import "fmt" func main() { a := 2 b := 5 fmt.Println(&a, &b) } ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ฃผ์†Œ๋ฅผ.. 2021. 10. 29.
๋ฐ˜์‘ํ˜•