Variables and Constants

λ³€μˆ˜/μƒμˆ˜μ˜ μ„ μ–Έκ³Ό μ΄ˆκΈ°ν™”

Before getting started

Constants

  • λ³€μˆ˜μ§€λ§Œ 값을 λ°”κΏ€ 수 μ—†λŠ” 것

    • Javascriptμ—μ„œλŠ” const!

Untype constant

  • νƒ€μž…μ΄ μ—†λŠ” μƒμˆ˜

    • ex)

      package main
      
      func main() {
       const name = "chloe"
      }

Type constant

  • νƒ€μž…μ΄ μžˆλŠ” μƒμˆ˜

    • ex)

      package main
      
      func main() {
        const name string = "chloe"
      }
  • λ‹Ήμ—°ν•˜μ§€λ§Œ μƒμˆ˜μ΄λ―€λ‘œ 값을 λ³€κ²½ν•  수 μ—†λ‹€!

    • ex)

      package main
      
      func main() {
        const name string = "chloe"
        name = "camila" // -> λΆˆκ°€
      }

Variables

  • 말 κ·ΈλŒ€λ‘œ λ³€μˆ˜ (값을 λ³€κ²½ν•  수 μžˆλ‹€)

    • Javascriptμ—μ„œλŠ” let!

Untype variable

  • νƒ€μž…μ΄ μ—†λŠ” λ³€μˆ˜

    • ex)

      package main
      
      import "fmt"
      
      func main() {
      
       var year = 2020
       fmt.Println(year)
      
      }

Type variable

  • νƒ€μž…μ΄ μžˆλŠ” λ³€μˆ˜

    • ex)

      package main
      
      import "fmt"
      
      func main() {
      
       var month int = 12
       // -> month := 12 (same)
      
       fmt.Println(month)
      }

Types in Go

  • GoλŠ” μž‘μ„±ν•œ κ°’μ˜ type을 μ•Œμ•„λ‚΄λ €κ³  ν•œλ‹€

    • GoλŠ” type langague 이기 λ•Œλ¬Έ!

      • "Type이 무엇이닀!" λΌλŠ” 것을 μ•Œλ €μ£Όμ–΄μ•Ό 함

        • Javaλ‚˜ typescript 처럼 νƒ€μž…μ„ μ•Œλ €μ€˜μ•Όν•¨

1. λ³€μˆ˜ μ„ μ–Έν•˜κΈ°

1-1. Goμ—μ„œμ˜ λ³€μˆ˜ μ„ μ–Έ 방식

var λ³€μˆ˜μ΄λ¦„ λ³€μˆ˜ν˜•

  • λ³€μˆ˜λ₯Ό μ„ μ–Έν•œ κ³³μ—μ„œ λ°”λ‘œ μ΄ˆκΈ°κ°’μ„ μ„€μ •ν•  수 있음

    ex)

    package main
    
    func main() {
      var num1 int = 1000
      var num2 int = 1413
    
      println("λ§μ…ˆμ„ ν•΄λ³΄μ•„μš” ~ ", num1 + num2)
    }

1-2. :=

  • Short Assignment Statement 라고 뢈림

  • type μ„ μ–Έ 없이 λ³€μˆ˜ μ„ μ–Έ κ°€λŠ₯

    • Goκ°€ type을 μ°Ύμ•„μ€€λ‹€!

      • μ΄λ ‡κ²Œ shorthand μ‚¬μš©ν•˜λ©΄ python처럼 μœ μ—°ν•˜κ²Œ μ‚¬μš© κ°€λŠ₯ν• λ“―!

  • ν•¨μˆ˜ μ•ˆμ—μ„œλ§Œ μ‚¬μš© κ°€λŠ₯!!

    ex)

    package main
    
    func main() {
     var a int = 100
     var b string = "Haha"
     c := 200
     print(b, a+c);
    }

+

GoλŠ” λ³€μˆ˜λ₯Ό λ§Œλ“€κ³  μ‚¬μš©ν•˜μ§€ μ•ŠμœΌλ©΄ μ—λŸ¬λ₯Ό λ°œμƒμ‹œν‚€λ―€λ‘œ μœ μ˜ν•˜μž!

Last updated