Go Packages
A package is a collection of related functions, types, and variables that were written by the Go team or by the community in order to solve a particular problem.
Most common packages
The most common packages are the following:
fmt
: for formatting and printing data,math
: for mathematical functions (e.g.math.Sin
),strings
: for manipulating strings (e.g.strings.Replace
),sort
: for sorting slices,io
: for input/output,os
: for operating system functions (e.g.os.Open
).time
: for time functions (e.g.time.Now
).- etc.
There are many more packages, and you will discover them as you go along.
Importing packages
In order to use a package, we need to import it.
import "fmt"
To import multiple packages at once, we can write:
import (
"fmt"
"math"
)
Using packages
Once a package is imported, we can use its functions, types, and variables. They are accessed using the dot notation:
fmt.Println("Hello, world!")
You may have noticed that Println
is capitalized; otherwise, it would not be exported, that is accessible to us, who are outside the package.
Golang main
package and main()
method
So far, in our examples, we have always begun our program by package main
. It is a special package where the compiler treats the program as executable code; else, it's considered a package archive. The main()
function that we have indicates that it is an entry point of an executable program.
package main
import "fmt"
func main() {
fmt.Println("Welcome to Go Programming!!!")
}
Nested Package
Nested packages are nothing but a package inside a package. We can create a nested package in Go by simply creating a subdirectory. We can import a nested package just like the normal package. For Example, Go has a math
package inside that we have a cmplx
package nested.
package main
import (
"fmt"
"math/cmplx"
)
func main() {
fmt.Printf("%.1f", cmplx.Abs(3+4i)) //5.0
}
Package Alias
We can provide an alias while importing the packages. This would be useful in certain situations like:
- You have multiple packages with the same name, which leads to conflict
- The package name is very lengthy
In these conditions, we can use package alias. We need to provide an alias before the package name, which becomes our variable reference for further usage.
In the following example, we have created an str
alias for the fmt
package and used str.Printf
to print the value.
package main
import (
str "fmt"
"math/cmplx"
)
func main() {
str.Printf("%.1f", cmplx.Abs(3+4i))
}
Unused packages
If you have imported a package but never used it in the program, the Go compiler will throw an error message "package imported but not used". In order to avoid the error, we can use the underscore _
as an alias.
In the following example, we are not using the math
package, but still, we have imported using underscore as an alias. This identifier tells the compiler to ignore the errors if the package is not used.
package main
import (
"fmt"
_ "math/cmplx"
)
func main() {
fmt.Println("Golang")
}
Installing more packages
If you want to use a package that is not part of the standard library, you need to install it first.
If it is distributed as a Git repository, you can use the go get
command:
go get github.com/your-username/your-package
We will learn more about creating custom packages in our advanced tutorial.