Could not import package

Hi. Error text :
could not import entities (cannot find package “entities” in any of
/usr/lib/go-1.20/src/entities (from $GOROOT)
/home/jk/go/src/entities (from $GOPATH))

I’ve simple structure
entities|entity.go
main.go

entities is a package. It tries to find along this way
/home/jk/go/src/entities (from $GOPATH))

But project located into /home/jk/go/src/go_test.

What I should to do for fix it?

package entities

type Entity struct {
	id   int
	name string
}

func (Entity) newEntity(id int, name string) *Entity {
	return &Entity{id, name}
}
package main

import (
	"fmt"
	"entities"
)

func main() {
	ent := entities.newEntity(1, "name")
	fmt.Println(ent)
}

Hi @Neverhood,

Go has no relative import paths—at least, not directly.

What is the module name that you gave your module? (It’s stored in go.mod, in the line that starts with module.)

Add the module name as a prefix to the import path:

import "<your module name>/entities".

This should make the import work.

(See also the Multiple Files example in the Go Playground. (The “files” are just sections in the editor widget, separated by -- filename.ext -- lines.) The module name is play.ground, the package foo lives in subdirectory foo/, and the resulting import path is play.ground/foo.)

thanks a lot!
If I understand you right - I can’t use some mechanism for writing short way like in C++?

#include "enitites" // instead path/entities

No, it does not get shorter than that. The module-based import path helps avoid ambiguities. Imagine you create a package named os and import it as os. The compiler would not be able to distinguish your os package from the os package in the standard library.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.