Work with Packages in Golang

Stay DRY with Go packages!

Photo by Brandable Box on Unsplash
The adorable Go gopher

What is a Go package?

package <package_name>

A simple example

import "fmt"
Using the fmt package

Package main

The main() function
$ go run main.go
$ go run /path/to/directory/of/main_package

Multiple and nested imports

Using multiple import statements
Using one import statement
Using the rand package

Export from packages

Export Greet() and not sayHello()

Create Go packages

$ mkdir pkg-tutorial
$ cd pkg-tutorial
$ go mod init
Project structure


Two simple functions: Sum() and Subtract()


Two simple functions: TitleCase() and lowerCase()


A simple constant: Welcome


The main() function
I love Go!
2 + 5 is 7
2 - 5 is -3
Hello World

Install third-party packages

go get
import ""

Final Thoughts

Incoming Software Engineer (Backend/Server) @ Shopee SG. If you are not a Medium member, you can read my articles on my website:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store