Golang: Package, Import

By Xah Lee. Date: . Last updated: .

The Main Package

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 + 4) // 7
}

Every go source code file have the above structure.

Import

import name

Import a package named name.

name is a string.

package main

import "fmt"
import "math"

func main() {
	fmt.Printf("%v\n", math.Sqrt(9)) // 3
}
import (name1; name2; etc)

Import multiple packages, with one single import statement.

package main

import ( "fmt"; "math" )

func main() {
	fmt.Printf("%v\n", math.Sqrt(9)) // 3
}

Imported Function Names Begin with Upper Case

Function names from imported packages always start with a Capital letter. e.g. fmt.Printf.