main
패키지에서 시작한다.package main
import (
"fmt" //포맷팅된 출력
"math/rand" //난수 생성
)
func main() {
fmt.Println("My favorite number is", rand.Intn(10))
}
fmt
, math/rand
두 개의 패키지를 사용함."math/rand"
내부의 파일들은 package rand
로 시작한다.설명
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))
}