설명
In Go, a name is exported if it begins with a capital letter. For example, Pizza is an exported name, as is Pi, which is exported from the math package.
pizza and pi do not start with a capital letter, so they are not exported.
When importing a package, you can refer only to its exported names. Any "unexported" names are not accessible from outside the package.
Run the code. Notice the error message.
To fix the error, rename math.pi to math.Pi and try it again.
Pi
는 math
패키지에서 export된다.package main
import (
"fmt"
"math"
)
func main() {
~~fmt.Println(math.pi)~~
fmt.Println(math.Pi)
}
에러메시지
package main
import "fmt"
func add(x int, y int) int {
return x + y
}
func main() {
fmt.Println(add(42, 13))
}
변수 이름 뒤에 타입이 선언된다.
반환 타입이 나중에 나오는게 코틀린과 비슷해보인다.
func add(a int, b int) //1
func add(a, b int) //2
package main
import "fmt"
func swap(x, y string) (string, string) { return y, x }
func main() {
a, b := swap("hello", "world")
fmt.Println(a, b)
}
swap
함수는 두 개의 string을 반환한다.