Module Management

goinstall → go get → tried rust cargo/dep, nope → go module

Old GOPATH way: The Go programming language initially delimited a scope for the location of dependencies and custom projects inside a filesystem. This was defined by the GOPATH environment variable. This means that Go would look for binaries and source code only under the directory pointed to by this variable.

There are three notable directories under GOPATH: src, pkg, and bin. The src directory holds the source code of both your projects and installed dependencies. When you execute a command such as go get github.com/user/repo, the Go tool fetches the module from the specified location and places it into the src directory under the GOPATH, with a path named after the resource's URL.

official proposal: https://go.googlesource.com/proposal/+/master/design/24301-versioned-go.md

There are three notable directories under GOPATH: src, pkg, and bin. The src directory holds the source code of both your projects and installed dependencies. When you execute a command such as go get github.com/user/repo, the Go tool fetches the module from the specified location and places it into the src directory under the GOPATH, with a path named after the resource's URL.

In Go, module names are used system-wide and therefore should be as specific as possible

Creating a new module:

  • create empty directory, and (e.g.) hello.go file as the source file with the package name

package hello

func Hello() string {
    return "Hello, world."
}

Now the directory contains a package but not a module because there's no go.mod file. We can

Example of go project structure

/home/user/projects/
└── myapp/                         # Your project
    ├── custom/                   # Custom packages
       └── ...                          # .go files defining functionality
    ├── go.mod                     # go.mod file defining dependencies & versions
    ├── go.sum                     # go.sum file that verifies dependency integrity
    └── main.go                    # Entrypoint

Imports

The go command resolves imports by using the specific dependency module versions listed in go.mod. When it encounters an import of a package not provided by any module in go.mod, the go command automatically looks up the module containing that package and adds it to go.mod, using the latest version.

"// indirect" comments mean that at the time the require directive was added, the module is not referenced directly in any of the module’s source files.

go.sum file stores checksum. not necessarily a pipfile.lock type file. see more

TADA

Like should the imports still be:

or just

answer: BOTH are fine good discussion: https://www.reddit.com/r/golang/comments/otb8p3/project_structure_for_golang_web_server_react/

Useful commands

  • go list -m all -------- lists current module and all its dependencies

  • go mod init --------- creates a new module, initializing the go.mod file that describes it

  • go mod tidy --------- removes unused dependencies

  • go get . ------- to add all dependencies for a package in your module

  • go get example.com/theirmodule ------ add a specific dependency

    • e.g. getting a remote module: go get github.com/spf13/cobra

  • go run main.go ------- runs the main.go file

Modules

  • functions to be called by outside the module must be capitalized

Last updated