我试图让我的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
,由我的Makefile
(make 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与一个直接链接到文档的工作是一个答案。
2条答案
按热度按时间7ivaypg91#
fivyi3re2#
$ sudo /home/我/bin/去获取github.com/melbahja/goph
GOROOT
GOROOT
。1.从go.dev/dl下载一个适合你的处理器架构的Go源码树(对于典型的Linux安装,通常是amd 64)
cd /usr/local
个1.将tarball解压缩为根目录
cd ~/bin
个ln -s /usr/local/go/bin/go go
个ln -s /usr/local/go/bin/gofmt gofmt
个cd
到您的项目目录中,并按照您设置Makefile
的方式进行构建。现在应该可以工作了。