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

Every go source code file have the above structure.

Import

There are 2 syntax to import. One is for importing a single package, the other can be used to import multiple packages in one shot.

import name
Import a package named name.
The name should be a string.
package main

import "fmt"
import "math"

func main() {
	fmt.Printf("%v\n", math.Sqrt(9)) // 3
}
import (name1; name2 )
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.

Golang

Compile and Run

String

Types and Values

Branching and Loop

Data Structure

Function

Misc

Examples

Reference