I know I am lacking a fundamental understanding how Go is seeking for a package, but let me just emphasize my thoughts and if needed - you could downvote.
This is my structure of the Go module:
├── go.mod
├── gopher.json
├── main.go
├── story.go
├── template.html
└── tests
├── cyow_test.go
└── gopher.json
Nothing too outside of the straightforward, dedicated /tests directory where the tests are supposed to be placed.
This is my cyow_test.go file:
import (
"io/ioutil"
"story"
"testing"
)
func TestUnmarshallOverStoryStruct(t *testing.T) {
t.Parallel()
content, fileError := ioutil.ReadFile("gopher.json")
if fileError != nil {
t.Error("The file for Chapter is not found.")
}
story := story.Story{}
fmt.Println("Story has been initialized")
err := json.Unmarshal([]byte(content), &story)
fmt.PRintln("Json unmarshall statement has been executed.")
if err != nil {
panic(err)
}
}
You could ignore the function, it's mainly for some learning purposes. The important part is that I am relying on a story package, which has been declared as part of the module.
When I go inside /tests and run 'go test' I receive:
cyow_test.go:5:2: package story is not in GOROOT (/usr/local/go/src/story)
I have ran 'go mod tidy' inside the module root directory and my simple questions are:
- Why Go does not find out the package by default ? It's a package part of the module, so it schould come natively - this is my assumption.
- Does that mean that the only way to refer to packages ( even inside your module ) is to reference them through a remote repo URL, like github.com ... or eventually just copy the package to /usr/local/go/src ( which is not friendly at all )
CodePudding user response:
Your assumption is wrong. The way to import a package is 'module/package'. The module name does not have to include a repository name. Do not copy packages to go source directory.