Go has included support for versioned modules as proposed here since 1.11 with the initial prototype as vgo. The concept of modules in Go was introduced to handle the problem of dependencies within your Go applications. Several packages are collected and combined to form a module which is stored in a tree-like structure in a file with a go.mod file at its root.
Note: Update current version of Go in your system to the latest one to work with Go modules.
Let us now see how to create modules in Golang. To create a module first create a directory and then go inside it using the following commands:
mkdir go_modules cd go_modules
To initialize the current directory as the root of the module that will allow us to manage dependencies, use the following command:
go mod init go_modules
As we are working outside the $GOPATH/src module, we need to explicitly specify the name of the module during initialization.
We can now check if the go.mod file is created and if present, the contents of it.
Next step is to create a simple Go file with the following code:
For testing the above function, we create another Go file with the following code:
After running the go test command, we should see that our test builds passed!
Adding dependencies to Go Module
To add dependencies to our module, we will use the require() function along with the version of the module being used:
All the dependencies in our package are shown as follows:
And all the files in our directory are listed below:
Note: The go command uses the go.sum file to ensure that future downloads of these modules retrieve the same bits as the first download and the modules your project depends on do not change unexpectedly.
- How to Create an Empty File in Golang?
- How to Create Custom Errors using New Function in Golang?
- How to create a Struct Instance Using a Struct Literal in Golang?
- How to Create and Print Multi Dimensional Slice in Golang?
- How to Create Your Own Package in Golang?
- How to convert a slice of bytes in uppercase in Golang?
- Golang program that uses fallthrough keyword
- math.Lgamma() Function in Golang with Examples
- math.Float64bits() Function in Golang With Examples
- How to check equality of slices of bytes in Golang?
- atomic.AddInt64() Function in Golang With Examples
- atomic.StoreInt64() Function in Golang With Examples
- reflect.FieldByIndex() Function in Golang with Examples
- string.Contains Function in Golang with Examples
- bits.Sub() Function in Golang with Examples
- How to convert a slice of bytes in lowercase in Golang?
- io.PipeWriter.CloseWithError() Function in Golang with Examples
- Import in GoLang
- time.Round() Function in Golang With Examples
- How to add a method to struct type in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.