我正在尝试编译我的 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环境变量。
# 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
二进制文件位于 ~/bin/go
。
指定依赖项(不列出无尽的子依赖项)并使该项目编译的最有效方法是什么?我想解决上面列出的所有编译问题。