I am creating my first project in golang. I am having trouble familiarizing with the concept of URL as package so I decided to make everything local.
My project structure is like this:
myproject
├── cmd
│  └── myapp
│  └── main.go
├── go.mod
├── pkg
│  └── api
│  └── serve.go
└── README.md
I tried to be inspired by this: https://github.com/golang-standards/project-layout/tree/master/cmd
I generated the go.mod using
cd ~/myproject
go mod init myproject/cmd/myapp
In my main.go I have:
package main
import (
"fmt"
"myproject/pkg/api"
)
func main() {
fmt.Println("Hello")
}
I run myapp using go run cmd/myapp/main.go
However I get:
cmd/myapp/main.go:5:5: package myproject/pkg/api is not in std (/usr/local/go/src/myproject/pkg/api)
>Solution :
go mod init myproject/cmd/myapp is incorrect, or at least, likely not what you intended. Whatever the module name is becomes the import root of your module, which means to import cmd/myapp you’d have to use myproject/cmd/myapp/cmd/myapp.
You most likely want go mod init myproject instead. You can also correct this manually by editing the module line in your go.mod.
Take a look at the go.mod file in the example project you referenced: https://github.com/golang-standards/project-layout/blob/master/go.mod
module github.com/YOUR-USER-OR-ORG-NAME/YOUR-REPO-NAME
Also see: