迁移到centos后编译golang报错

Error in compiling golang after migrating to centos

我正在将我的 Golang 程序从 windows 迁移到 Centos 7

它在 Windows
中完美运行 但是当我尝试 在 centos 上编译时,我得到类似

的错误
main.go:20:3: cannot find package "github.com/BurntSushi/toml" in any of:
        /usr/local/go/src/github.com/BurntSushi/toml (from $GOROOT)
        /root/work/src/github.com/BurntSushi/toml (from $GOPATH)
main.go:15:3: cannot find package "github.com/dgrijalva/jwt-go" in any of:
        /usr/local/go/src/github.com/dgrijalva/jwt-go (from $GOROOT)
        /root/work/src/github.com/dgrijalva/jwt-go (from $GOPATH)
main.go:16:3: cannot find package "github.com/gwlkm_service/config" in any of:
        /usr/local/go/src/github.com/gwlkm_service/config (from $GOROOT)
        /root/work/src/github.com/gwlkm_service/config (from $GOPATH)

对 centos 有点陌生所以不知道该怎么做

看来您已经配置了 GOPATH,没有 Go Module,您可以使用 go get [package path] 下载导入包。

go get github.com/BurntSushi/toml
go get github.com/dgrijalva/jwt-go
go get github.com/gwlkm_service/config

正是

你在服务器上的 go 安装似乎在 /usr/local/go 中并且 你的项目在 /root/work 所以你所有的依赖项都应该在 /root/work/src/usr/local/go/src

现在开始行动,用 运行ning echo $GOPATH 检查你的 GOPATH 假设它自动设置为 /usr/local/go/src 如果没有,请遵循 - How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?

如果一切正常,那么在您的文件夹中 运行 go mod init

这将创建 mod 文件,这将帮助您进行进一步的安装 调查 - https://blog.golang.org/using-go-modules

然后运行去获取上面@beiping96说的命令

go get github.com/BurntSushi/toml go get github.com/dgrijalva/jwt-go go get github.com/gwlkm_service/config

注意 - 完成上述过程后,您将生成 go modules 文件(与 package.json 相同),以后您将不需要关心依赖关系