Go Packages and Import

All package files, should be in the same (single) directory. i.e. all package source code files should be located in a one single directory. All files in a specific folder should belong to a one single package. It’s a convention, not a rule.

package clause can be used only once per file and it should be the first line in .go source file.

Package contains multiple Go files belonging to same folder.

Package Types

In Go, the packages can be divided into 2 categories: Executable Packages and Library Packages.

Main Package

To make a package executable, name that package main. It’s a special package. Any package that is intended to run on a command-line, must declare package main.

main package: is used to generate the executable binary, and the main function is the entry point of the program. Take hello.go as an example:

package main

import "greet"

func main() {
  greet.Greet()
}

Library Package

Another type is library package. This category can also include 2 types: Library packages and Special packages.

Normal Library Package

Library package: is used to generate the object files that can be reused by others. Take greet.go as an example:

package greet

import "fmt"

func Greet() {
  fmt.Println("नमस्ते किंशुक!")
}
  • We can only import them.
  • These are created only for reusability purposes.
  • Package name can have any name.
  • Doesn’t need to have function named main(). To avoid confusion, it’s better not to have function named main() in a reusable package.

Special Library Package

Some other packages for special purposes, such as testing.

Nearly every program needs Go standard ($GOROOT) or third-pary ($GOPATH) packages. To use them, you should use import statement:

import "fmt"
import "github.com/NanXiao/stack" 

Or:

import (
  "fmt"
  "github.com/NanXiao/stack"
)

In the above examples, the “fmt” and “github.com/NanXiao/stack” are called import path, which is used to find the relevant package.

Importing the packages

Import statement examples:

import "fmt"
import "math/rand"

Or grouped:

import (
    "fmt"
    "math/rand"
)

Import Aliasing

You may also see the following cases:

import m "lib/math" // use m as the math package name
import . "lib/math" // Omit package name when using math package

If the go install command can’t find the specified package, it will complain the error messages like this:

… : cannot find package “xxxx” in any of: /usr/local/go/src/xxxx (from $GOROOT) /root/gowork/src/xxxx (from $GOPATH)

To avoid library conflicts, you’d better make your own packages’ path the only one in the world: E.g., your github repository destination:

github.com/NanXiao/… Conventionally, your package name should be same with the last item in import path; it is a good coding habit though not a must.

Function init()

  • The init() function is used to initialize the state of a package.
  • Go automatically calls init() function before calling command-line package’s main() function.

Reference: The Go Programming Language. https://github.com/NanXiao/golang-101-hacks https://github.com/aditya43/golang#strings-runes-and-bytes