DEV Community

Cover image for Go - (10) Packages & Modules
Chathumi Kumarapeli
Chathumi Kumarapeli

Posted on

Go - (10) Packages & Modules

Packages

Main package

If you see package main at the top of a go file, you have been writing your code within the main package. It has the entry point to the main function. The main package runs as a standalone programme. So, the executable code should be written within the main package.

All other packages (any package whose name is not main) are library packages. You can use (import) those libraries in your code. Library packages export named functions that other libraries can use.

package main

// imports from the standard library
import (
    "fmt"
)

func blah() {
    fmt.Println("=====> Hello there!")
}
Enter fullscreen mode Exit fullscreen mode

Libraries

You can import standard libraries by their name. Like we have done to "fmt". But the import statement is different if you have published your library and want to use it in another code. There, you should give the path to your remote repository of that library.

// myrepo is the custom library
import (
    "github.com/myusername/myrepo"
)
Enter fullscreen mode Exit fullscreen mode

Package name is the same as the last element of its import path. It is not a rule but a best practice.

In Go, if all of the code exists in the same directory, it's part of the same package. Because in Go, we don't export or import code in the files of the same directory (package). So, you want to import code only if they are in a different directory/package. Directory and package are the same in Go, because you can have multiple packages in the same directory.

Modules

A module is a collection of packages that are released together.

A Go repository usually has only one module. It is located at the root of the repository.

The module of a repo is declared by the go.mod file, which is at the root. It contains,

  • module path
  • Go version
  • external package dependencies (optional)

In Go, a module is given by its remote repository path where the module is hosted. It tells Go where it can download the module from.

An import path is the combination of the module path and package subdirectory.

Packages in the standard library do NOT have a module path prefix.

$GOPATH is an environment variable whose value is set by default in a location on your machine. Usually, it is ~/go. But since we are working with Go modules, we can avoid putting our code into the $GOPATH/src directory.

Go includes a remote URL in module paths to simplify remote downloading of packages.

There are two ways to run a Go programme.

  • go run main.go: This is suitable for a very small code. It simply runs the code. Go run can accept package names and file names are arguments.
  • go build: This produces a production executable. It gives a compiled binary that can run anywhere.

If you execute go install inside your repository, Go installs your application globally on your machine. Now, you can run your binary by simply giving its name from anywhere on your machine.

Initialize a module

Create a directory named mymodule and cd into it. Then run below:

  • remote: something like github.com
  • username: Github namespace
go mod init <remote>/<username>/mymodule
Enter fullscreen mode Exit fullscreen mode

It creates a new go.mod file.
Then you can start writing your code inside it with a new package mymodule. This can be imported into other packages of your main project. But for that, make sure you start the names of your functions in Capital letters. Lowercase names are not exportable in Go.

Running go build on a library package compiles it and silently saves it to the local build cache.


Top comments (0)