Skip to content

Package basics

Every Go file belongs to a package. A package is a collection of Go files in the same directory that are compiled together. The main package defines an executable program. Other packages are reusable libraries.

Create two files in the same directory: main.go and math.go with package mymath.

// math.go
package mymath
func Add(a, b int) int {
return a + b
}
// main.go
package main
import (
"fmt"
"mymath"
)
func main() {
fmt.Println(mymath.Add(3, 4))
}
Terminal window
7