Golang: Package, Import

By Xah Lee. Date: . Last updated: .

Every Go program is made up of packages.

Programs start running in package named “main”, with function “main”.

package main

import "fmt"

func main() {
        fmt.Println(3)
        fmt.Println("xyz")
        fmt.Println(true)
}

Import

The name should be a string.

package main

import (
        "fmt"
        "math"
)

func main() {
        fmt.Println(math.Sqrt(9)) // 3
}

Exported Names, Begin with Upper Case

A name is exported if it begins with a capital letter.

When importing a package, you can refer only to its exported names. Any “unexported” names are not accessible from outside the package.

package main

import (
        "fmt"
        "math"
)

func main() {
        fmt.Println(math.Pi)
}

Reference

The Go Programming Language Specification - The Go Programming Language#Packages