golang

Golang Import Local Package

In Go, a package refers to a collection of Go source code files in the same directory that are compiled together. Typically, you will use these packages to export and import code between them. This in turn makes the code modular and easy to maintain.

However, if you are just getting into the Go programming language, it can be quite tricky to figure out how to import local package. That is what this tutorial is about.

Golang Package

To better understand what is a go package, let us take a project called my workspace. Inside the workspace directory, you have 3 more directories called dir1, dir2 and dir3.

Hence, the directory tree is as shown:

└───workspace
    ├───dir1
    ├───dir2
    └───dir3

This means that the workspace project has 3 packages. Each file created under each directory becomes a part of that package (directory).

Keep in mind that the main project is what is known as the main package. In most cases, you will find files such as main.go with the first line of code as”

package main

Going back to the workspace project, you will the first entry in each of the files with the name of the package under which they belong.

An example is as:

package dir1 // for files in dir1

package dir2 // for files in dir2

package dir3 // for dir3

Every file within a package can export its code. Other files within the project can then reference that package (directory) and import the code from the files. This allows you to just import a single package and all the code in the files under that package will be accessible for use.

To create a new package in your project, you can just create a new directory.

Let us look at a practical example for creating go packages.

Golang Initialize Module

The first step when import packages is to initialize a new module. You can do that by running the command:

go mod init <module_name>

For example, in the workspace directory, we can create a new module as:

go mod init workspace

Once you run the go mod init, it creates a go.mod file which keeps track of modules you import. The file also contains information such as the module name, go version, etc.

Think of it like the package.json file in Node.

Mind the naming of your modules as you will require it when importing your packages. An example of go.mod file is as shown:

module workspace

go 1.17

Since we do not have any external packages, the file contains the module name and the go version.

Golang Create Package

As mentioned, to create a package in your go project, create a new directory and add the source code files under that package.

For example, let us create go source files for dir1, dir2 and dir3 packages.

$ touch dir1/dir1.go

$ touch dir2/dir12.go

$ touch dir3/dir3.go

At the first line of each file, include the package namespace as:

package dir1 // replace dir2 with the name of the package

Next, let us add some code in each of these files.

// dir1.go
package dir1
funcSayHello() string {
    return "Hi from package dir1"
}
//dir2.go
package dir2
funcSayRunning() string {
    return "I am running from package dir2"
}
// dir3.go
package dir3
funcSayBye() string {
    return "Goodbye from package dir3"
}

The above examples are simple. They are just functions that return a string value from each package.

NOTE: To export a variable or a function from a go package, ensure you start the name of the function or variable with an uppercase letter.

Again: ☝️☝️☝️☝️☝️

Golang Import Local Packages

The last step is to import your local packages so you can use the code in each one of them. In the root of your project, aka, the workspace directory, create a main.go file.

Next, add the following lines to import your local packages:

package main
import (
"fmt"
    "workspace/dir1"
    "workspace/dir2"
    "workspace/dir3"
)
funcmain() {
    fmt.Println(dir1.SayHello())
    fmt.Println(dir2.SayRunning())
    fmt.Println(dir3.SayBye())
}

In the example above, we add 3 import clauses to import all the packages in our program.

Once imported, we can use the exported code in the packages.

We can then run the code in main.go file as:

$ go run main.go
Hi from package dir1
I am running from package dir2
Goodbye from package dir3

You will see the output as exported by the functions from the packages.

Conclusion

In this article, we discussed how you can export and import code using Go packages. Packages are a very practical and clean way to organize your code and retain readability and maintenance. We hope you enjoyed!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list