I'm having problems to use packages. I a following literally the steps I find online, but I have an error message.
I have this package in GOPATH:
go/src/greet/day.go
package greet
var morning = "Good morning"
var Morning = "hey" + morning
I want to import it in my code:
go/src/app/entry.go
package main
import ("fmt"
"greet")
func main(){
fmt.Println(greet.Morning)
}
When I run entry.go, I receive this message: entry.go:4:3: package greet is not in GOROOT (/usr/local/go/src/greet)
Does anybody how to fix it?
Thank you.
GOPATH isn't really used anymore. You can use a different directory and run go mod init greet
. This will create a new module "greet" in that folder, and from within that module you can import packages using import "{module name}/{package path}". It is a best practice to use the folder name as the package name, so the import path matches the folder names (except for main packages).
Additionally, if your module lives in a git repository, your module name should be the path to the git repository. for example, go mod init github.com/jaenmo/myrepo
.
within your module make a folder for your main package. You should be able to import using your module name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.