Reputation: 289
I am a beginner at GO, and try to run my first project. And I have the problem to import my own package. I create go project under go/src. And the file structure is below:
- projectName
- function
- utils
helper.go( package utils)
main.go(package main)
And I want to import utils in the main
import (
"projectName/function/utils"
)
Give me the error that could not import projectName/function/utils (cannot find package "projectName/function/utils" in any of /usr/local/go/src/projectName/function/utils (from $GOROOT) /Users/myUser/go/src/projectName/function/utils (from $GOPATH)
And I created the project exactly under /Users/myUser/go/src/ folder. More information: go.mod is created under /Users/myUser/go/src/projectName And the content is:
module projectName
go 1.16
if I run "go mod tidy", it will throw the error:
projectName imports
projectName/function/utils: package projectName/function/utils is not in GOROOT (/usr/local/go/src/projectName/function/utils)
Upvotes: 0
Views: 2192
Reputation: 289
Once you create a new project, please run go mod init projectName
, otherwise, you can't import the package.
Please don't skip this step.
Upvotes: 1