Home

Creating a Go Package

By Joel Dare - Written November 15, 2022

Some quick instructions on how to create a package and a module in Go (GoLang).

Your directory of files will look like this.

example/
  main.go
  special/
    thing.go

Your code will look like this.

main.go

package main

import "example/special"

func main() {
	special.Thing()
}

special/thing.go

package special

func Thing() {
	println("Special Thing")
}

Online Packages

If you call your package something like joeldare.com/example instead of example then you can put the package files online and it can be loaded from that URL instead of the local directory. To do that you would change the init command to something like go mod init joeldare.com/example. In that case, your main.go might look like this.

package main

import "joeldare.com/example/special"

func main() {
	special.Thing()
}

It will still load the special package from the special/ directory if it’s there but if not it will look for it online at joeldare.com/example/special.

Build your next site in pure HTML and CSS

Want to build your next site in pure HTML and CSS? Join the free Five-Day Neat Starter Email Course and build a lean, production-ready page before Friday.

Email Me the Crash Course


JoelDare.com © Dare Companies Dotcom LLC

Terms - Privacy