DEV Community


Golang Set GOPATH and GOROOT

Divyanshu Shekhar
Developer | Blogger
・2 min read

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.

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.

Golang HTTP

import (

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.

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.

Read the whole post on Golang GOPATH and GOROOT from the original Post.

Discussion (0)