GoLang Modules

Reading material

  1. Tutorial: Create a Go module: https://go.dev/doc/tutorial/create-module
  2. How to write Go code: https://go.dev/doc/code
  3. https://go.dev/blog/using-go-modules
  4. https://go.dev/ref/mod

Command to initiate a module inside a directory

go mod init example.com/greetings

go mod init myexample.com/generate-code-from-openapi-spec

The name of the directory for the module doesn’t matter. The name of the module - from go.mod file matters.

Dependencies on external modules

Add the import statements in the go files and run the command go mod tidy in the root directory.

The go mod tidy command adds missing module requirements for imported packages and removes requirements on modules that aren’t used anymore.

How to build

To build the binary, run: go build in the root folder.

It will put a binary file in the same folder with the name of the module (defined in go.mod).

How to run

Run it with ./hello


Links to this note