Declaration and imports

Packages are methods of organizing code in go. Packages should be designed to do a single task. Declare what package a file is in at the top of the file with

package packagename

You can import a package to use with the import keyword. This is automatically handled by running go mod tidy.

import (
	"name"
	"namespace/packageName"
)

Public and private

Only functions and types that begin with a capital letter will be accessible outside of the package.

func PublicFunction() {
	
}
 
func privateFunction() {
 
}

By default (and by convention) you need to do name.Function() to access items in a package, unless you use a . as in import . "name". Although importing this way is usually frowned upon because certain common functions across packages (like package.New()) could conflict.

If you wish you can also rename packages by adding a name before the package name in the import statement.

Modules

Modules are a collection of packages that form a single application/program. In go we use the go.mod file which is managed through the CLI.

One of the first things you’ll do when setting up a go project is create the module with go mod init github.com/eboyden42/nameOfModule.