Go > Packages and Modules > Using Packages > Importing packages
Basic Package Import
This example demonstrates how to import a standard library package in Go and use its functions. It imports the fmt
package for printing to the console and the math
package for mathematical operations.
Code Demonstration
This code snippet showcases a basic import statement. We import the fmt
package, which provides formatting and printing functionalities, and the math
package, which offers mathematical functions. The main
function then uses functions from both packages: math.Sqrt
to calculate the square root and fmt.Printf
to display the result.
package main
import (
"fmt"
"math"
)
func main() {
num := 16.0
sqrt := math.Sqrt(num)
fmt.Printf("The square root of %.1f is %.1f\n", num, sqrt)
}
Concepts Behind the Snippet
In Go, packages are collections of related code that are grouped together for organization and reusability. The import
statement allows you to use code defined in other packages within your own program. The package names specified in the import
statement must correspond to the directory name where the package source code is located (relative to the GOPATH/src
or using Go Modules). The parenthesis after the import
keyword is used to group multiple package imports into a single block, improving readability.
Real-Life Use Case
Imagine you are building a web application that requires handling JSON data. You would import the encoding/json
package to marshal and unmarshal JSON data. Similarly, if you need to perform network operations, you would import the net/http
package. Packages provide pre-built functionalities, saving you from writing common code from scratch.
Best Practices
.
or alias
syntax. Using .
imports all the package's exports into the current file's namespace, which can lead to naming conflicts and is generally discouraged. Aliasing with a different name is useful if you want to avoid name collisions.
Interview Tip
Be prepared to explain the purpose of packages and the import
statement. Also, understand the different import styles, especially the grouped import and the common gotcha of unused imports. Be able to discuss the implications of using dot imports.
When to Use Them
Use packages whenever you need to reuse code across multiple files or projects. Packages promote modularity, maintainability, and code reusability. Importing is fundamental to using external libraries and the standard library. Without imports, even basic tasks like printing to the console would be impossible.
Alternatives
There are no real alternatives to using packages for code reuse in Go. All non-trivial Go programs rely on packages and the import
mechanism. Vendoring (or using Go Modules) is an alternative approach to managing dependencies, but it complements rather than replaces the import
statement.
Pros
Cons
FAQ
-
What happens if I import a package but don't use it?
The Go compiler will generate an error if you import a package but don't use any of its exported functions, types, or variables. This is a deliberate design choice to encourage clean and efficient code. -
How do I import a package from a specific location on my filesystem?
With Go modules, you simply import the package using its module path, and Go will handle the dependency resolution. If you are not using modules, you need to make sure the package is located within yourGOPATH/src
and use its path relative toGOPATH/src
in the import statement. However, Go modules are the recommended approach.