Packages in Go

GoLanger
2 min readJul 23, 2017

--

Packages, Variables, and Functions are important building blocks of Go Programming Language. Let us discuss about packages in detail here.

Packages

Any programs in Go start with declaring the package name (i.e, commonly we use main to declare it as a main package) and which can be used anywhere in the program. Packages allow us an option to include packages / modules which is built by someone else or community for specific functionalities. And Go itself have a lot of built in packages for multiple operations and functionalities.

A package can be included to the program using the import statement.

At time we will be required to use multiple packages in a same file. That can be done using the following method.

Another important thing is we have to use some remote packages for certain advanced functionalities. Go provides the best and easiest method to use remote packages in programs. Initially we have to include the package to our project folder using the go get command in terminal. So, a remote package can be included using the following method. Here I’m importing the gorilla mux for reference. It is used as a URL router and dispatcher for golang.

go get -u github.com/gorilla/mux

Now we will be able to import the remote package to our code block using the import statement itself.

Go is very rich of packages for most of the functionalities you are going to use. It will be better to use or inspire the go packages when you write a program. It will give more insights about good and efficient programming practices.

You will be free to write your own packages and include it in your project depends on the use case. I will cover the topic “Building custom packages in Go” soon.

--

--