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 github.com/jseow5177/pkg-tutorial
Project structure

math.go

Two simple functions: Sum() and Subtract()

casing.go

Two simple functions: TitleCase() and lowerCase()

welcome.go

A simple constant: Welcome

main.go

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

Install third-party packages

go get github.com/gorilla/csrf
import "github.com/gorilla/csrf"

Final Thoughts

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

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