Go Packages

Packages

The following example creates a custom package.

$ mkdir -p go/src/local/mypkg
$ touch go/src/local/sample.go
$ touch go/src/local/mypkg/mypkg.go
.
└── go
    └── src
        └── local
            ├── mypkg
            │   └── mypkg.go
            └── sample.go

Create mypkg.go with the following content. The package statement declares the package name.

package mypkg

import "fmt"

func FuncA() {			 // Names starting with uppercase letters are exported automatically.
    fmt.Println("FuncA()")
}

func funcB() {			 // Names starting with lowercase letters are not exported.
    fmt.Println("funcB()")
}

Create sample.go with the following content. FuncA() starts with an uppercase letter and is exported, so it can be used. funcB() starts with a lowercase letter and is not exported.

package main

import "local/mypkg"

mypkg.FuncA()		 // Available
mypkt.funcB()		 // Error