[CentOS系统下]:How to fix Go 'import "fmt": cannot find package'

xingyun86 2019-10-29 1987

Tagged as: golang

Are you getting the error: 'import "fmt": cannot find package'? The error is quite easy to solve with a simple change to the GOPATH environment variable.

When setting up go (golang), and running go build on the hello world file, you may experience the error:

hello.go:3:8: import "fmt": cannot find packagepackage .
    imports runtime: import "runtime": cannot find package

It may also manifest as something like the text below if you use go run.

go build fmt: exec: "/home/user/go/pkg/tool/linux_386/pack": stat /home/user/go/pkg/tool/linux_386/pack: no such file or directory
go build runtime: exec: "/home/user/go/pkg/tool/linux_386/pack": stat /home/user/go/pkg/tool/linux_386/pack: no such file or directory

Your problem if you encounter these errors, is that your GOROOT is set to what your GOPATH should be set to. If you unset your go root and set your GOPATH:

export GOPATH=$GOROOTunset GOROOT

Your file should work (go run hello.go):

Hello, world.

You may also want to add the GOPATH env variable to your .bashrc and delete the GOROOT variable if it's there.


×
打赏作者
最新回复 (0)
查看全部
全部楼主
返回