Advertisement
In this blog, we will take a look at how Golang imports packages in order to access the code functionality. In the previous blog, we looked at the organization of code into Golang Packages, Workspace, and Environment variables like GOROOT and GOPATH.
Golang Import
The import statement tells the go compiler where to look on the local disk in order to find the package you wish to import.
Packages are imported in the go file using the import keyword.
import "fmt"
The above import keyword tells the compiler that in order to compile that program the compiler needs to access the Golang fmt package.
The Import keyword lets the compiler know that the program needs to reference the code contained within the package at that file location.
Golang Multiple Imports
If you need to import more than one package, there are two ways:
- Many Import Keywords
import "fmt"
import "os"
import "net/http"
Using import keywords for every package import makes our code messy and thus the most common way to import many packages is using the import block.
- Golang Import Block
import (
"fmt"
"os"
"net/http"
)
The Golang Import block gives a clean look to our program and also easy to access.
Golang Import Package
While writing the first go program we used the import keyword, now let’s take a look at how the go compiler looks for the packages.
Golang Packages reside on the disk based on their relative path to the directories referenced by the Go environment.
Packages in the standard library are found under where Go is installed on the machine i.e C:/Go by default.
Custom Golang Packages reside in the GOPATH, which is the workspace for packages.
Example:
GOROOT = C:/go/
GOPATH = C:/Users/%USERPROFILE%/go
import “fmt”
The above example imports fmt package.
The Go Compiler will first search for the imported package in GOROOT/src/fmt, If the search is unsuccessful then it will move to GOPATH and search for the fmt package there.
The compiler will stop searching once it finds a package that satisfies the import statement.
Here the search is successful in GOROOT and the compiler imports the package.
If the compiler searches the GOPATH and never finds the package that was imported in the file, an error will be raised when you try to run or build your program.
Some of the IDE will let you know that the import path is not correct in the code writing process and it makes coding easy for the developers.
Golang import types
Let’s see different types of Golang imports that we can implement in our program.
Golang Remote Imports
The Go Programming language is a new language and it also has some latest features like importing some packages from the distributed version control systems like GitHub, Launchpad, and Bitbucket.
Golang tooling has built-in support for fetching source code from these sites. Golang Import relative path makes it easy for us to distinguish between the local imports and remote imports.
Example:
import (
"fmt" // Local import
"net/http" // Local import
"github.com/divshekhar/go/strutils" // Remote Import
)
Golang remote import path
After the remote import is done in Golang, the go build command will take a look at the import paths, the local imports and the remote imports are different as the remote import contains a URL.
When the go build command fails to import the package as the package is not present in our local machine, so we need to get that package from that URL and save it to our local machine. This is done by the go-get command.
go get
The go get command present in go tools will be used to fetch the package from the DVCS (Distributed Version Control System) and stores it into the GOPATH at the location that matches the import URL.
go get command is recursive and thus it can fetch all the source code and dependencies for a package.
Golang Named Imports
While importing multiple packages in a go file, there is a possibility that the custom or remote package name may be the same and thus it results in ambiguity. To resolve this ambiguity, Golang Named Imports are used.
Example:
import (
"fmt"
myfmt "format/fmt" // Custom Package
)
The above example uses fmt package that comes as a part of the standard library and the second imported package name is also fmt that is created by the user. If we try to access the exported functions from both fmt packages we can’t do it as both have the same name.
Golang helps to resolve this import ambiguity by renaming the imported package and thus that package is now accessed using the renamed name.
Golang Blank Identifier
According to Golang Conventions, any import statement declared and not used will raise an error. This Convention is applied in Golang to eliminate code bloat from packages that are imported and not used. This keeps our code clean and lightweight.
Although sometimes we think of the future and import some packages in advance that we wish to use, but this raises an error as the imports are not used at that time so we need something that skips that import statement. The Answer is the Blank Identifier.
Blank Identifier The _ (underscore character) is known as the blank identifier and has many uses within go. It’s used when you want to throw away the assignment of a value (read-only value) or ignores return values from a function.
Example:
package main
import (
"fmt"
"net/http"
)
func main() {
fmt.Println("Hello World")
}
Error:- imported and not used: “net”
Using Blank Identifier
package main
import (
"fmt"
_ "net/http"
)
func main() {
fmt.Println("Hello World")
}
Output:
Hello World
Hope you like it!
Also, read Why Golang is called the future of Server-side language?
Learn more about Golang Imports from the official Documentation.