Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

package myproject/pkg/api is not in std (/usr/local/go/src/myproject/pkg/api)

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading