project-layout icon indicating copy to clipboard operation
project-layout copied to clipboard

where are go.mod and go.sum files

Open mattharrigan opened this issue 5 years ago • 6 comments
trafficstars

Hello,

I'm new to go and have what I think is an extremely basic question: Where are the go.mod and go.sum file or files? Edit: and a follow on question, can you provide some example of go.mod files?

Thanks

mattharrigan avatar Jan 17 '20 14:01 mattharrigan

@mattharrigan hi! 🤝

First, check your Go version. It can be v1.11 or higher.

Next, type in console (on root of your Go project):

foo@bar:~/project$ go mod init github.com/user/project-name

Basic output of this CLI command is file go.mod:

module github.com/user/project-name // module name

go 1.13 // version of Go

Note: you may create go.mod file by CLI at any time you want, not necessarily at the very beginning of development!

If root folder have some Go files, go.mod can contain something like this:

module github.com/user/project-name

go 1.13 

require (
	// list of dependencies for your project, for example: 
	github.com/urfave/cli/v2 v2.1.1
	gopkg.in/src-d/go-git.v4 v4.13.1
)

File go.sum will be created automatically. It's like package-lock.json in Node.js, but for Go Modules.

Hope this helps 👍

koddr avatar Jan 17 '20 15:01 koddr

so typically there is a single go.mod and go.sum? i have go 1.13

mattharrigan avatar Jan 17 '20 15:01 mattharrigan

@mattharrigan typically this files can be at root of your Go project.

foo@bar:~/project$ tree .
.
├─ main.go   <-- file with some Go code
├─ go.mod    <-- file with dependencies
├─ go.sum    <-- (automatic generated) file with locked module info

For example, please look at one of my template Go project (for net/http): https://github.com/create-go-app/net_http-go-template

koddr avatar Jan 17 '20 15:01 koddr

Now that Go 1.14 is out and Go Modules are officially ready for production it makes sense to update the repo though keep in mind that this repo is not meant as a template for projects with default files, etc. Makefile in the root directory is empty, so go.mod and go.sum are likely going to be empty too in this repo. Sounds like there's value in having a set of actual templates though. The only gotcha there is that they end up being pretty opinionated (which is by design), so there'll be more people unhappy about how those templates look like and what they include and don't include :-)

kcq avatar Mar 11 '20 05:03 kcq

I could use some help along this topic. I would like to follow the example folder structure of this repo, but i'm not sure how to properly set up my go modules when I have multiple applications.

.
├── go.mod
├── go.sum
├── pkg
│   ├── packageA
│   │   └──packageA.go
│   └── packageB
│       └──packageB.go
└── cmd
    ├── app1
    │   └── main.go
    └── app2
        └── main.go

Specifically how can I have app1/main.go reference pkg/packageA of the feature branch that I'm on? Do I need to use replace?

Also please note this is a private GitHub repository (not sure if that matters)

Any guidance and/or links would be greatly appreciated.

nicklofaso avatar Sep 10 '20 13:09 nicklofaso

go.sum <-- (automatic generated) file with locked module info

go.sum is not a lock file.

peterbourgon avatar Dec 30 '21 01:12 peterbourgon