I'm new to go modules, and am taking them for a spin in a new project which I'm trying to model after the structure described here
Here is an example of my directory structure:
.
├── cmd
│ └── app_name
│ └── main.go
├── go.mod
├── go.sum
├── internal
│ └── bot
│ └── bot.go
└── pkg
├── website_name
│ ├── client.go
│ ├── client.options.go
│ ├── server.go
│ └── server.options.go
└── lib
└── lib.go
- Is this idiomatically correct? I know there's not a whole lot of consensus out there, but I'd like to follow best practices.
- When I run
go build
I get 'unexpected module path "github.com/ragurney/app_name/cmd/app_name"', but when I rungo build ./...
it works. Why?
When I move main.go
to the top level everything works as expected. Should I just not use the /cmd
pattern with modules?
module
line in your go.mod? It sounds like it's referring tocmd/app_name
which would be incorrect - it should point at the directory where it is located. – Crymotherapycmd/app_name
specifically:module github.com/ragurney/app_name/cmd/app_name
. So instead it should be:github.com/ragurney/app_name
? Doing that causes a similar error: "cannot find module providing package github.com/ragurney/app_name" – Spitsbergengithub.com/ragurney/app_name
if that's the root (where the go.mod file is). Do you have multiplego.mod
files in your repo by chance? – Crymotherapy