go: module basics

2021-11-13

 | 

~1 min read

 | 

140 words

Adding an module to your Go program requires registering it in the go.mod file.

For the basics, see: Get started with Go.

However, here’s a simple example for a project called test-proj running go 1.17:

  1. Initialize a go.mod with go mod init
  2. Create a module
  3. Import the module into your main go function.

In the above screen shot, I do some things in a non-idiomatic way (e.g., naming the file for my main.go file as index.go) to prove a point.

On the left, I have my module. In the middle, I have my main function which leverages my module (and aliases it as u). On the right is my go.mod.

The file structure is visibile on the right and in the terminal we can see that I’m able to run the project with go run index.go.



Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!