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

๊ฐœ์š”

  • go ์–ธ์–ด ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž
  • ๋‹ค์Œ ๋ชฉ์ฐจ๋กœ ์ง„ํ–‰๋˜๋ฉฐ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ฒฝํ—˜ํ•ด๋ณธ ์‚ฌ๋žŒ ๋Œ€์ƒ์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค
    • ๊ฐœ์š”
    • ๋ณ€์ˆ˜, ์ƒ์ˆ˜ ์„ ์–ธ(+์ถ•์•ฝํ˜• ์„ ์–ธ)
    • ํƒ€์ž…
    • ํ•จ์ˆ˜ ์„ ์–ธ(ignore value(_), defer, export function)
    • ์กฐ๊ฑด๋ฌธ(if, switch)
    • ๋ฐ˜๋ณต๋ฌธ(for)
    • pointer(*)
    • ๋ฐฐ์—ด(slice, array)

๋ณ€์ˆ˜, ์ƒ์ˆ˜ ์„ ์–ธ

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์ˆ˜๋Š” const, ๋ณ€์ˆ˜๋Š” var ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด ์„ ์–ธํ•œ๋‹ค
  • ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณ€์ˆ˜, ์ƒ์ˆ˜ ์„ ์–ธ๋„ ๊ฐ€๋Šฅํ•œ๋ฐ, ์ด๋Š” ์•„๋ž˜ ์ฝ”๋“œ๋กœ ์‚ดํŽด๋ณด์ž
// ๊ธฐ๋ณธ ์„ ์–ธ
var age = 23

var name string
name = "mokhs"

// ๋ณ€์ˆ˜ ์ถ•์•ฝํ˜• ์„ ์–ธ : ์•„๋ž˜์„œ ์ž์„ธํžˆ ์•Œ์•„๋ณด์ž
role := "Backend Engineer"

// ์ƒ์ˆ˜ ์„ ์–ธ
const minAge = 0

fmt.Println(age, name, role, minAge)

์ถ•์•ฝํ˜• ์„ ์–ธ

  • go ์—์„œ๋Š” name := "mokhs00"์™€ ๊ฐ™์ด ์ถ•์•ฝํ˜• ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
  • ๋Œ€์ž…ํ•˜๋Š” ๊ฐ’์œผ๋กœ ํƒ€์ž…์ด ์ถ”๋ก ๋˜์–ด ์ž๋™์œผ๋กœ ์ ์šฉ๋œ๋‹ค.
  • ์ด๋Š” ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , ํ•จ์ˆ˜ ์™ธ๋ถ€์—์„œ๋Š” ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ˆ ์ฃผ์˜ํ•˜์ž
// error
count := 1

func main() {
  count := 1

  fmt.Println(count)
}

ํƒ€์ž…

  • go์—๋Š” ํƒ€์ž…์ด ์กด์žฌํ•˜๊ณ  ๊ณต์‹๋ฌธ์„œ์—์„œ๋„ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค
var Typ = []*Basic{
  Invalid: {Invalid, 0, "invalid type"},

  Bool:          {Bool, IsBoolean, "bool"},
  Int:           {Int, IsInteger, "int"},
  Int8:          {Int8, IsInteger, "int8"},
  Int16:         {Int16, IsInteger, "int16"},
  Int32:         {Int32, IsInteger, "int32"},
  Int64:         {Int64, IsInteger, "int64"},
  Uint:          {Uint, IsInteger | IsUnsigned, "uint"},
  Uint8:         {Uint8, IsInteger | IsUnsigned, "uint8"},
  Uint16:        {Uint16, IsInteger | IsUnsigned, "uint16"},
  Uint32:        {Uint32, IsInteger | IsUnsigned, "uint32"},
  Uint64:        {Uint64, IsInteger | IsUnsigned, "uint64"},
  Uintptr:       {Uintptr, IsInteger | IsUnsigned, "uintptr"},
  Float32:       {Float32, IsFloat, "float32"},
  Float64:       {Float64, IsFloat, "float64"},
  Complex64:     {Complex64, IsComplex, "complex64"},
  Complex128:    {Complex128, IsComplex, "complex128"},
  String:        {String, IsString, "string"},
  UnsafePointer: {UnsafePointer, 0, "Pointer"},

  UntypedBool:    {UntypedBool, IsBoolean | IsUntyped, "untyped bool"},
  UntypedInt:     {UntypedInt, IsInteger | IsUntyped, "untyped int"},
  UntypedRune:    {UntypedRune, IsInteger | IsUntyped, "untyped rune"},
  UntypedFloat:   {UntypedFloat, IsFloat | IsUntyped, "untyped float"},
  UntypedComplex: {UntypedComplex, IsComplex | IsUntyped, "untyped complex"},
  UntypedString:  {UntypedString, IsString | IsUntyped, "untyped string"},
  UntypedNil:     {UntypedNil, IsUntyped, "untyped nil"},
}

ํ•จ์ˆ˜ ์„ ์–ธ

  • ํ•จ์ˆ˜ ์„ ์–ธ์€ func ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•œ๋‹ค
  • ๊ฐ’์„ ์—ฌ๋Ÿฌ๊ฐœ return ํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ๊ฐœ ํƒ€์ž…์„ ํ•œ ๋ฒˆ์— ์„ ์–ธํ•˜๋Š” ๋“ฑ์˜ ๋ฐฉ์‹๋„ ๊ฐ€๋Šฅํ•˜๋‹ค ์ด๋Š” ์•„๋ž˜์„œ ์•Œ์•„๋ณด์ž
// ํ•จ์ˆ˜ ์„ ์–ธ๊ธฐ๋ณธ ํ˜•์‹

/*
func ํ•จ์ˆ˜๋ช…(์ธ์ž) ๋ฐ˜ํ™˜ํƒ€์ž… {
  return ๋ฐ˜ํ™˜ํƒ€์ž…๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฆฌํ„ด๊ฐ’
}
*/

func foo(value int) int {
  return value
}

// ์ธ์ž ์—ฌ๋Ÿฌ๊ฐœ ๋ฐ›๊ธฐ
func foo(value ...int) []int {
  return value
}

// ์—ฌ๋Ÿฌ ๊ฐ’ return
func foo(value int) (int, string) {
  return value, "SUCCESS"
}

// naked return : return ๊ฐ’์„ ๋ฏธ๋ฆฌ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Œ
func foo() (value int, message string) {
  value = 1
  message = "SUCCESS"
  return
}

ignore value(_)

  • ๋ฐ˜ํ™˜๊ฐ’์„ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•ด์•ผ ํ•˜์ง€๋งŒ, _๋ฅผ ์ด์šฉ ํ•ด๋‹น ๋ฐ˜ํ™˜๊ฐ’์„ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์ฐธ๊ณ ๋กœ _๋Š” ํ‚ค์›Œ๋“œ๋กœ ์ทจ๊ธ‰๋˜์–ด ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค
  • ๋‹ค์Œ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž
func main() {
  _, b := foo()
}

func foo() (int, int) {
  return 1, 2
}

defer

  • defer๋ฅผ ์ด์šฉํ•ด ํ•จ์ˆ˜ ์ข…๋ฃŒ ์‹œ์— ์‹คํ–‰ํ•  ๊ตฌ๋ฌธ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค
  • defer๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ตฌ๋ฌธ์„ ์ถ”๊ฐ€ ์‹œ์— ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ ํ›„์ž…์„ ์ถœ(LIFO)๋ฐฉ์‹์œผ๋กœ ์‹คํ–‰๋œ๋‹ค
func foo() (int, int) {
  defer fmt.Println("isFinished")
  return 1, 2
}

export function

  • ํ•จ์ˆ˜๋ฅผ export ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ํ•จ์ˆ˜์˜ ์ฒซ ๊ธ€์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค
  • go์˜ built-in package์˜ ํ•จ์ˆ˜๋“ค์—์„œ ์ฒซ ๊ธ€์ž๊ฐ€ ๋Œ€๋ฌธ์ž์ธ ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋Š” ๊ฐ™์€ ์ด์œ ์ด๋‹ค

์กฐ๊ฑด๋ฌธ(if, switch)

  • if, switch ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•œ ์กฐ๊ฑด๋ฌธ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
  • ๋‹ค๋ฅธ ์–ธ์–ด๋“ค๊ณผ ์œ ์‚ฌํ•˜๋ฉฐ, ์ฐจ์ด์ ์ด ์žˆ๋‹ค๋ฉด ์กฐ๊ฑด๋ฌธ ์„ ์–ธ๊ณผ ๋™์‹œ์— ํ•ด๋‹น ์กฐ๊ฑด๋ฌธ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž
// if
func canIView(age int) bool {
  // if๋ฌธ ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜(koreanAge) ์„ ์–ธ
  if koreanAge := age + 2; koreanAge < 18 {
    return false
  }
  return true
}

// switch
func canIView2(age int) bool {

  switch koreanAge := age + 2; {
  case koreanAge < 18:
    return false
  default:
    return true
  }
}

switch, fallthrough

  • go์˜ switch๋Š” case๋งˆ๋‹ค break๋ฌธ์˜ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์—ฐ์‚ฐ์ด ์ข…๋ฃŒ๋˜๋Š” ๋‹ค๋ฅธ ์–ธ์–ด์™€๋Š” ๋‹ค๋ฅด๊ฒŒ break๋ฌธ์ด ์—†์–ด๋„ case์˜ ์กฐ๊ฑด๊ณผ ์ผ์น˜ํ•œ๋‹ค๋ฉด switch ์—ฐ์‚ฐ์„ ๋น ์ ธ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค
  • ์ด๋Š” go ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ case๋งˆ๋‹ค break๋ฌธ์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค
  • breakํ•˜๊ณ  ์‹ถ์ง€ ์•Š๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด fallthrough ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด breakํ•˜์ง€ ์•Š๊ณ  ์•„๋ž˜ case๋กœ ๋‚ด๋ ค์ค„ ์ˆ˜ ์žˆ๋‹ค
  • ์ด ๋•Œ ์ฃผ์˜ํ•  ์ ์€ ์•„๋ž˜ case์— ์ผ์น˜ํ•˜๋Š”์ง€ ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด case ์•ˆ์˜ ์—ฐ์‚ฐ์„ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค
func main() {
  v := 1
  switch v {
  case 1:
    fmt.Println("1")
    fallthrough
  case 2: // 2์™€ ์ผ์น˜ํ•˜์ง€ ์•Š์ง€๋งŒ fallthrough์— ์˜ํ•ด์„œ case ์™€ ์ผ์น˜ํ•  ๋•Œ์™€ ๋™์ผํ•œ ์—ฐ์‚ฐ ์ˆ˜ํ–‰
    fmt.Println("2")
    fallthrough
  default:
    fmt.Println("default")
  }

  /* ์ถœ๋ ฅ
  1
  2
  default
  */
}

๋ฐ˜๋ณต๋ฌธ(for)

  • ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•  ๋•Œ for, while์˜ ํ‚ค์›Œ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ
  • go์—์„œ ๋ฐ˜๋ณต๋ฌธ์€ for๋งŒ ์กด์žฌํ•˜๊ณ  for๋งŒ์œผ๋กœ while์˜ ๊ธฐ๋Šฅ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค
  • ๋˜ํ•œ ์กฐ๊ฑด๋ฌธ์—์„œ์™€ ๋™์ผํ•˜๊ฒŒ ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค
  • range๋ฅผ ์ด์šฉํ•˜๋ฉด ์—ฐ์†์ ์ธ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ณ€์ˆ˜๋ฅผ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์ž์„ธํ•œ ๊ฑด ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž
// ๊ธฐ๋ณธ for๋ฌธ
func main() {
  for i:=0; i < 10; i++ {
    // ...
  }
}

// range๋ฅผ ์ด์šฉํ•œ ๋ฐ˜๋ณต๋ฌธ
func addAll(numbers ...int) int {
  total := 0

  // range๋ฅผ ์ด์šฉํ•œ numbers ์ˆœํšŒ numbers์˜ ํƒ€์ž…์€ []int
  for _, v := range numbers {
    total += v
  }

  return total
}

pointer

  • C์™€ ๋™์ผํ•˜๊ฒŒ ํฌ์ธํ„ฐ(pointer) ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
  • ํฌ์ธํ„ฐ๋ž€ ํ•ด๋‹น ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์„ ์˜๋ฏธํ•˜๊ณ  ์ฃผ์†Œ๊ฐ’์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ฐธ์กฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•„์š”์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ๋กœ์ง์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์ฃผ์†Œ๊ฐ’์„ ์˜๋ฏธํ•˜๋Š” &, ํ•ด๋‹น ์ฃผ์†Œ์— ์žˆ๋Š” value๋ฅผ ์˜๋ฏธํ•˜๋Š” *ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•œ๋‹ค
  • ์ž์„ธํ•œ ๊ฑด ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด์„œ ์•Œ์•„๋ณด์ž
func main() {
  a := 2
  // b์— a์˜ ์ฃผ์†Œ๊ฐ’์„ ์ €์žฅ
  b := &a
  a = 10
  // 0xc00006ef60 0xc00006ef60
  println(&a, b)
  // *ํ‚ค์›Œ๋“œ๋กœ ์ฃผ์†Œ์— ์ €์žฅ๋œ value ์กฐํšŒ
  // 10 10
  println(a, *b)
}

๋ฐฐ์—ด(slice, array)

  • go๋Š” ๋ฐฐ์—ด์„ ๋‚˜ํƒ€๋‚ด๋Š” built-in ์ž๋ฃŒํ˜•์œผ๋กœ slice, array๋ฅผ ์ œ๊ณตํ•œ๋‹ค
  • slice์™€ array์˜ ์ฐจ์ด์ ์€ ์šฉ๋Ÿ‰(๋ฐฐ์—ด์˜ ํฌ๊ธฐ)๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ, array๋Š” ๊ณ ์ • ํฌ๊ธฐ, slice๋Š” ์œ ๋™์ ์ธ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋ฉฐ, ์„ ์–ธ๋ฐฉ๋ฒ•๋„ ๋‹ค๋ฅด๋‹ค
    (+ slice๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฐฐ์—ด pointer,length, capacity ๊ฐ’์„ ๊ฐ€์ง„๋‹ค)
  • ์Šฌ๋ผ์ด์‹ฑ, ์š”์†Œ ์ถ”๊ฐ€(append), ๋ณต์‚ฌ(copy)๊ธธ์ด ์ธก์ •(length), ์šฉ๋Ÿ‰ ์ธก์ •(capacity) ๋“ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
  • ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด์„œ ์ž์„ธํžˆ ์•Œ์•„๋ณด์ž
func main() {
  slice := []int{1, 2, 3}
  array := [5]int{1, 2, 3}

  // [1 2 3] [1 2 3 0 0]
  fmt.Println(slice, array)

  // built-in ํ•จ์ˆ˜์ธ append()๋ฅผ ์ด์šฉํ•œ ์š”์†Œ ์ถ”๊ฐ€, slice์—๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค
  // [1 2 3 3 4 5]
  fmt.Println(append(slice, 3, 4, 5))
  // slice์— ๋‹ค๋ฅธ slice๋ฅผ appendํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ
  // [1 2 3 5 6 7]
  fmt.Println(append(slice, []int{5, 6, 7}...))

  target := []int{0, 0, 0}
  // target := make([]int, len(slice), cap(slice))
  // target ๊ณผ slice์˜ length๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ณต์‚ฌ๋˜์ง€ ์•Š์œผ๋‹ˆ ์ฃผ์˜!!
  // 3 [1 2 3] [1 2 3]
  fmt.Println(copy(target, slice), target, slice)

  // ๊ธฐ์กด slice๋‚˜ array๋ฅผ ์Šฌ๋ผ์ด์‹ฑ (ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•๊ณผ ์œ ์‚ฌํ•˜๋‹ค)
  sliceOfArray := array[:2]

  // len : length ๊ธธ์ด, cap : capacity ์šฉ๋Ÿ‰
  // [1 2] 2 5
  fmt.Println(sliceOfArray, len(sliceOfArray), cap(sliceOfArray))
}

slice ์„ ์–ธ with make()

  • slice์— ๋Œ€ํ•ด์„œ ์ข€ ๋” ์•Œ์•„๋ณด์ž
  • slice๋Š” go์˜ built-in function์ธ make()๋กœ๋„ ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
  • make()๋ฅผ ์ด์šฉํ•˜๋ฉด slice์˜ ์ดˆ๊ธฐํ™” ๊ธธ์ด(length), ์šฉ๋Ÿ‰(capacity)๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค
  • length๋ฅผ 0์•„๋‹Œ ๊ฐ’์œผ๋กœ ์ง€์ •ํ•˜๋ฉด length ๊ฐ’ ๋งŒํผ ๊ธฐ๋ณธ ๊ฐ’์ด ์ €์žฅ๋˜๋ฏ€๋กœ ์ฃผ์˜! (์•„๋ž˜ ์˜ˆ์‹œ ํ™•์ธ)
func main() {
  // make(type, length, capacity)
  s := make([]int, 5, 10)
  // 5 10 [0 0 0 0 0]
  fmt.Println(len(s), cap(s), s)
}

slice capacity

  • ๋ณดํ†ต ๋Œ€๋ถ€๋ถ„์˜ ์–ธ์–ด์—์„œ ๋™์  ๋ฐฐ์—ด(e.g.slice)์€ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋งˆ๋‹ค ์šฉ๋Ÿ‰์„ ๋™์ ์œผ๋กœ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค
  • go์—์„œ๋Š” ์ง€์ •ํ•œ capacity๋ฅผ ์ดˆ๊ณผํ–ˆ์„ ๋•Œ ๊ธฐ์กด capacity๋ฅผ 2๋ฐฐ๋กœ ๋Š˜๋ฆฐ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ๋ณต์‚ฌํ•œ๋‹ค
  • ์ฆ‰ ์œ„ ๊ณผ์ •์œผ๋กœ ์ธํ•ด์„œ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— slice์˜ ์šฉ๋Ÿ‰์„ ์ ์ ˆํžˆ ์„ ์–ธํ•ด์•ผํ•œ๋‹ค

map

  • key, value ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” map์„ ์ œ๊ณตํ•œ๋‹ค
  • ์„ ์–ธ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค
func main() {
  // map[key type][value type]
  fooMap := map[string]string{"key": "value"}

  fmt.Println(fooMap)
}

structs

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

type user struct {
  name string
  age  int
}

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

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

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

Golang - Rate Limit  (0) 2023.09.16
Go : ๊ณ ๋ฃจํ‹ด(goroutine), ์ฑ„๋„(channels)  (0) 2022.03.20
Go : struct, type, method  (0) 2022.03.20