In this blog, we will take a look at how code is organized in Golang into packages, how to interact with Golang packages, and how to Set GOPATH and GOROOT.
Packages are one of the most important concepts in Golang. Golang Packages helps us to separate semantic units of functionality into different packages. This also enables Code reusability and data control from each package.
Learn Golang Basics:
- Variables in Golang
- Datatypes and Operations in Golang
- Constants in Golang
- Golang Arrays and Slices
- Go Maps and Structs
- Golang If-else and Switch Statements
- Go for loop
- Golang Defer, Panic and Recover
Golang Packages
The Go Programs are organized into groups of files called packages so that the code has the ability to be imported into other Go Projects as smaller reusable pieces.
import (
"net/http"
)
Let’s See how the imported packages are organized.
Golang Package Structure
Let’s look at the Golang net and net/http package, which enables Golang to use HTTP functionality.
import net import net/http
The Path C:/Go/src/net and C:/Go/src/net/http are the directories where the go code is organized as Golang Packages. These directories contains file with .go extension and some more directories. The Go files inside these directories contains code for a particular work.
Each Package can be imported and used individually so that developers can import only the specific functionality that they need.
Example: In order to implement HTTP servers, clients, and web utilities in our go program we need to import just net/http in our go program file.
All .go files must declare the package that they belong to as the first line of the file excluding white spaces and comments.
Suppose you are creating a Golang custom package. The Package name is strutils and thus .go file should contain the first line as the package name.
package strutils
You may not have multiple packages in the same directory, nor may you split a package across multiple directories.
This means that all the .go files in a single directory must declare the same package name.
Golang Packages Naming Convention
The package is known by the directory and thus it becomes important to declare it by a meaningful name, the name that describes the package work. This makes the user clear about the package and why should the user import it.
Let’s take the example of net/http package.
The Package name itself makes it clear what is the use of the HTTP package, i.e to enable the functionality of the HTTP server.
All the Files contained within the package should contain the same package name on the top.
Example: Let’s look at the net/http/server.go file.
The Server.go File is inside the HTTP package and thus the first line excluding white spaces and comments is the name of the package to which it belongs.
Line Number 7 in the image.
7 package http
While Naming the Package and the directories, use the lowercase short and concise name, because the package and file name will be repeated many times.
One thing to keep in mind is that the Package name may or may not be unique (Unique Package names is not strictly required), this is because the packages are imported using its full path and there will not be any kind of conflict in package names.
Golang Commands vs Packages
Commands in Golang refer to any executable program while Packages in Golang refer to an importable semantic unit of functionality.
Golang Package Main
The Golang package main is a special type of package. Any program which includes package main, when build produces a binary executable.
A Golang file should have package main and a main() function, else when build using go tools it will not generate binary executable.
The main() function is the entry point for the program. If the go file excludes the main function, it means the program will not have any kind of entry point and the file will not generate an executable.
The directory name of the file will be given to the binary executable file when the build is successful.
Golang Set GOPATH
The Golang GOPATH is an environment variable that has all the source files of the Go Programming language.
When any go file imports some of the local packages, the compiler looks for those imported files in the GOPATH/pkg directory and downloads the remote imports at that location.
go env
This is a go tool, that lets us know about the environment variables and modify it. Let’s take a look how to know the GOPATH and how to modify it.
go env GOPATH
The First Image shows what is our Golang GOPATH and GOROOT.
Second Image is the GOPATH directory.
The GOPATH contains the bin and pkg directory already when the go installs.
We need to make an src directory and all the source files will reside there. The Golang pkg directory stores all the custom packages, and the bin directory keeps the binary executables when we run go install command.
Golang Set GOPATH
$ set GOPATH=D:\go
This will set the Golang GOPATH to the D:\go path.
GOROOT can also be set using the same command, just change the GOPATH to GOROOT. It is advised not to change the GOROOT. You can change GOPATH to create your workspace.
But with newer update of Golang, you can create your GO Workspace anywhere without even changing the GOPATH.
UNSET Go Environment Variables
$ go env -u GOBIN
This will unset the Golang GOBIN Environment Variable.
Golang Project Workspace
Go to GOPATH/src/ and create a directory naming your project and inside it create the .go files.
Here we are creating hello.go and the file contains:
package main
import "fmt"
func main() {
fmt.Println("Hello Go")
}
Building the file using go build command from within the GOPATH/src/hello/ directory.
On Linux and Mac the generated file will be binary and On Windows the generated file will be executable.
Hope you like it!
Also, read Why Golang is called the future of Server-side language?
Learn more about Golang Packages from the official Documentation.
Learn Golang Advanced Topics: