解决:包foo不在GOROOT中

dgiusagp  于 5个月前  发布在  Go
关注(0)|答案(2)|浏览(59)

我试图让我的Go项目编译,但修复编译错误的方法并不明显。下面是简化的例子。

// filename: ~/myprojects/automate_things/automate_things.go
package main
import (
    "flag"
    "fmt"
    "github.com/melbahja/goph"
    "github.com/gleich/logoru"
)

func main() {
  // insert many things here
}

字符串
现在我使用make dep(下面是Makefile)构建一个go.mod并尝试获取所有依赖项,但这并没有获取它们,因为我一直看到package foo is not in GOROOT错误。

# filename: ~/myprojects/automate_things/Makefile
GOOS=linux

GO_SOURCE_FILE=automate_things.go
GO_BINARY_FILE=automate_things
GO_BIN_DIR=bin/

.SILENT: build
.DEFAULT_GOAL := build

build:
        -make fmt
        make dep
        go build $(GO_SOURCE_FILE)
        if [ "$(PLATFORM)" = "Linux" ]; then \
                GOARCH=amd64 GOOS=linux go build -ldflags "-s -w" -o ./$(GO_BINARY_FILE) $(GO_SOURCE_FILE); \
        elif [ "$(PLATFORM)" = "Darwin" ]; then \
                GOARCH=amd64 GOOS=darwin go build -ldflags "-s -w" -o ./$(GO_BINARY_FILE) $(GO_SOURCE_FILE); \
        fi
.PHONY: build

fmt:
        go fmt $(GO_SOURCE_FILE)
.PHONY: fmt

dep:
        -rm go.mod
        -rm go.sum
        go mod init automate_things
        go mod tidy
        go mod download
.PHONY: dep


这是我的~/.bashrc,它导出了几个相关的go env变量。

# filename: ~/.bashrc
# Build the default GOROOT (re-run the command as root)

mkdir -p /usr/local/go
export GOPROXY=https://proxy.golang.org
mkdir -p ~/gobin/bin
export GOPATH=~/gobin
#export GO111MODULE=auto

export PATH=$PATH:$GOPATH/bin


这是go.mod,由我的Makefilemake dep)生成

// go.mod
module automate_things

go 1.20

require (
        github.com/gleich/logoru v0.0.0-20230101033757-d86cd895c7a1
        github.com/melbahja/goph v1.4.0
)

require (
        github.com/fatih/color v1.10.0 // indirect
        github.com/kr/fs v0.1.0 // indirect
        github.com/mattn/go-colorable v0.1.8 // indirect
        github.com/mattn/go-isatty v0.0.12 // indirect
        github.com/pkg/errors v0.9.1 // indirect
        github.com/pkg/sftp v1.13.5 // indirect
        golang.org/x/crypto v0.6.0 // indirect
        golang.org/x/sys v0.5.0 // indirect
)


这是我看到的,当我make dep

$ make
make[1]: Entering directory '/home/me/myprojects/automate_things'
go fmt automate_things.go
make[1]: Leaving directory '/home/me/myprojects/automate_things'
make[1]: Entering directory '/home/me/myprojects/automate_things'
rm go.mod
rm go.sum
go mod init automate_things
go: creating new go.mod: module automate_things
go: to add module requirements and sums:
        go mod tidy
go mod tidy
go: finding module for package github.com/melbahja/goph
go: finding module for package github.com/gleich/logoru
go: found github.com/gleich/logoru in github.com/gleich/logoru v0.0.0-20230101033757-d86cd895c7a1
go: found github.com/melbahja/goph in github.com/melbahja/goph v1.4.0
go mod download
make[1]: Leaving directory '/home/me/myprojects/automate_things'
../../gobin/pkg/mod/golang.org/x/[email protected]/ssh/transport.go:8:2: package bufio is not in GOROOT (/home/me/src/bufio)
../../gobin/pkg/mod/github.com/mattn/[email protected]/noncolorable.go:4:2: package bytes is not in GOROOT (/home/me/src/bytes)


因此,让我们尝试直接使用go get和sudo下载,以消除任何潜在的权限问题。

$ sudo /home/me/bin/go get github.com/melbahja/goph
go: downloading github.com/melbahja/goph v1.4.0
go: downloading github.com/pkg/errors v0.9.1
go: downloading github.com/pkg/sftp v1.13.5
go: downloading golang.org/x/crypto v0.6.0
go: downloading github.com/kr/fs v0.1.0
go: downloading golang.org/x/sys v0.5.0
github.com/melbahja/goph imports
        context: package context is not in GOROOT (/usr/local/go/src/context)
github.com/melbahja/goph imports
        errors: package errors is not in GOROOT (/usr/local/go/src/errors)
github.com/melbahja/goph imports

提问

我的go二进制文件在~/bin/go中。
Makefile中获取/指定依赖项(不列出无休止的子deps)并使该项目编译的最有效方法是什么?我想修复上面列出的所有编译问题。
RTFM不是一个答案。RTFM与一个直接链接到文档的工作是一个答案。

7ivaypg9

7ivaypg91#

  • 正确安装Go。
  • 不要设置GOROOT
  • 不要设置GO111MONOTE
  • 执行一次mod init
  • 在添加依赖项(例如,使用go get)后进行go mod tidy,然后检查go.mod和go.sum
  • 使用Go Build构建
fivyi3re

fivyi3re2#

$ sudo /home/我/bin/去获取github.com/melbahja/goph

  • 问题的标题(以及最初的问题正文)提到GOROOT
  • 你是谁?
  • 我完全不知道为什么Go语言仍然在错误消息中提到GOROOT
  • 简而言之,你的Go安装似乎坏了...修复它:

1.从go.dev/dl下载一个适合你的处理器架构的Go源码树(对于典型的Linux安装,通常是amd 64)

  1. cd /usr/local
    1.将tarball解压缩为根目录
  2. cd ~/bin
  3. ln -s /usr/local/go/bin/go go
  4. ln -s /usr/local/go/bin/gofmt gofmt
  5. cd到您的项目目录中,并按照您设置Makefile的方式进行构建。现在应该可以工作了。

相关问题