Import local packages without GOPATH [SOLVED]


Written By - Tuan Nguyen
Advertisement

Introduction

In today's post, we are going to answer the question "Can we import the local packages without the GOPATH". We already have an article about how to organize your code and packaging structure in Golang. We also introduced 2 important environment variables: GOPATH and GOROOT:

The GOPATH environment variable lists places to look for Go code. On Unix, the value is a colon-separated string. On Windows, the value is a semicolon-separated string. 

GOPATH must be set to get, build and install packages outside the standard Go tree.

$GOPATH is the location for packages we create and third-party packages that we may have imported.

If the environment variable is unset, GOPATH defaults to a subdirectory named "go" in the user's home directory ($HOME/go on Unix, %USERPROFILE%\go on Windows), unless that directory holds a Go distribution. Run "go env GOPATH" to see the current GOPATH.

Import local packages without GOPATH [SOLVED]

 

 

 

Code organization

Go programs are organized into packages. A package is a collection of source files in the same directory that are compiled together. Functions, types, variables, and constants defined in one source file are visible to all other source files within the same package. Your module specifies dependencies needed to run your code, including the Go version and the set of other modules it requires.

A repository contains one or more modules. A module is a collection of related Go packages that are released together. A Go repository typically contains only one module, located at the root of the repository. A file named go.mod there declares the module path: the import path prefix for all packages within the module. The module contains the packages in the directory containing its go.mod file as well as subdirectories of that directory, up to the next subdirectory containing another go.mod file (if any).

Note that you don't need to publish your code to a remote repository before you can build it. A module can be defined locally without belonging to a repository. However, it's a good habit to organize your code as if you will publish it someday.

Advertisement

Start by creating a Go module. In a module, you collect one or more related packages for a discrete and useful set of functions. For example, you might create a module with packages that have functions for doing financial analysis so that others writing financial applications can use your work. For more about developing modules, see Developing and publishing modules.

 

Import local packages under the GOPATH

Under the GOPATH/src/ folder, we will create a test workspace. Take a look at the workspace project as an example to help you better understand what a go package is. There are two folders named: pack1 and pack2 inside the workspace directory. We also create a sub-folder inside pack1. To create folders, run the following command on Linux:

mkdir pack1 // for files in pack1

For example, let us create go source files for the pack1 and the main file:

touch pack1/helper1.go
touch ./main.go

Here is the folder architecture:

Import local packages without GOPATH [SOLVED]

Let's now add some code to helper1.go and main.go. Noted that at the first line of each file, include the package namespace as:

package pack1

 helper1.go:

package pack1

import "fmt"

func SayHello() {
	fmt.Println("Hello everyone from package1")
}

The last step is to import your local packages so you can use the code in each one of them. In the main package, create a main.go file. Next, add the following lines to import your local packages:

package main

import (
	"workspace/pack1"
)

func main() {
	pack1.SayHello()
}

Output:

Advertisement
Hello everyone from package1

 

Import local packages without the GOPATH

Both local and external package management has been simpler since the advent of go.mod. Go projects can also be created outside of the GOPATH by using go.mod.

First of all, we need to create a folder demo and run the command below to generate the go.mod file:

go mod init demo

This is the project structure inside the demo directory:

Import local packages without GOPATH [SOLVED]

For the demo purpose, add the following code in the helper1.go file:

package package1

import "fmt"

func SayHello() {
	fmt.Println("Hello everyone from package1")
}

In main.go, we can import the SayHello() function by referencing to "demo/src/package1":

package main

import (
	"demo/src/package1"
	"fmt"
)

func main() {
	fmt.Println("Hello from the main package")
	package1.SayHello()
}

Output:

Advertisement
Hello from the main package
Hello everyone from package1

 

Summary

The concept of a "local package" doesn't exist. Any parent/child relationships between packages are orthogonal to how packages are arranged on a disk. The dependency tree, which typically does not reflect the directory tree, is the only true hierarchy created by packages. If your code already under the GOPATH/src folder, just simply directly import it with its path. Or with go1.11 you can use the new modules system. go mod init <module_name> and then just import "<module_name>/<pkg_name>".

 

References

https://go.dev/doc/code
How to import local packages without gopath - Stack Overflow

 

Categories GO

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment