Managing Go Modules

Managing Dependencies with go.mod

A project’s dependencies are declared in the go.mod file. This is where the modules are stored when you do go get <module name>. You can remove unused dependencies using go mod tidy and your go.mod wil…


This content originally appeared on DEV Community and was authored by bashbunni

Managing Dependencies with go.mod

A project's dependencies are declared in the go.mod file. This is where the modules are stored when you do go get <module name>. You can remove unused dependencies using go mod tidy and your go.mod will only include all used imports.

When you clone a Go project from github, for example, you'll want to run go get to install all of the dependencies declared in the go.mod file. If you're coming from a background in web development, this is similar to how you run npm install to install all of the dependencies declared in your package.json file.

Indirect Dependencies

You may see some dependencies in your go.mod file that are indirect (denoted by // indirect). This means that one of your dependencies doesn't have its own go.mod file, so, the dependencies that it imports are included in your project's go.mod file.

For example:
Let's say we're building a Twitch bot, we need github.com/gempir/go-twitch-irc/v2, but it doesn't have a go.mod file and it uses functions from github.com/gempir/go-twitch-irc. You would then have github.com/gempir/go-twitch-irc // indirect in your project's go.mod file.

note: the above is a hypothetical scenario, you won't actually have any indirect dependencies with github.com/gempir/go-twitch-irc/v2

What is go.sum?

go.sum includes the checksum for the dependencies to make sure that you're installing the exact same dependency as the author used.

Managing your Projects as Go Modules

To create a new Go project where you can have a go.mod file, you need to set it up on your GOPATH with a name for your project.

Running go mod init <module name>
Your module name can be your project name or it can be a link to your github repo, just remove the https:// portion of the url.

You need to run go mod init before you're able to get dependencies for your project with go get


This content originally appeared on DEV Community and was authored by bashbunni


Print Share Comment Cite Upload Translate Updates
APA

bashbunni | Sciencx (2021-12-09T21:21:17+00:00) Managing Go Modules. Retrieved from https://www.scien.cx/2021/12/09/managing-go-modules/

MLA
" » Managing Go Modules." bashbunni | Sciencx - Thursday December 9, 2021, https://www.scien.cx/2021/12/09/managing-go-modules/
HARVARD
bashbunni | Sciencx Thursday December 9, 2021 » Managing Go Modules., viewed ,<https://www.scien.cx/2021/12/09/managing-go-modules/>
VANCOUVER
bashbunni | Sciencx - » Managing Go Modules. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/09/managing-go-modules/
CHICAGO
" » Managing Go Modules." bashbunni | Sciencx - Accessed . https://www.scien.cx/2021/12/09/managing-go-modules/
IEEE
" » Managing Go Modules." bashbunni | Sciencx [Online]. Available: https://www.scien.cx/2021/12/09/managing-go-modules/. [Accessed: ]
rf:citation
» Managing Go Modules | bashbunni | Sciencx | https://www.scien.cx/2021/12/09/managing-go-modules/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.