英文:
open /bin/migrate: operation not permitted when building Go tags
问题
我正在遵循这个Go + GraphQL教程https://www.howtographql.com/graphql-go/4-database/,但在安装和运行迁移时遇到了问题。
整个命令链是这样的:
go get -u github.com/go-sql-driver/mysql
go build -tags 'mysql' -ldflags="-X main.Version=1.0.0" -o $GOPATH/bin/migrate github.com/golang-migrate/migrate/v4/cmd/migrate/
cd internal/pkg/db/migrations/
migrate create -ext sql -dir mysql -seq create_users_table
migrate create -ext sql -dir mysql -seq create_links_table
但具体在以下部分:
go build -tags 'mysql' -ldflags="-X main.Version=1.0.0" -o $GOPATH/bin/migrate github.com/golang-migrate/migrate/v4/cmd/migrate/
cd internal/pkg/db/migrations/
我会在终端中收到以下错误:
go build github.com/golang-migrate/migrate/v4/cmd/migrate: copying /var/folders/f9/d6pn7fz92w53vcpywqd_08zm0000gp/T/go-build1656176552/b001/exe/a.out: open /bin/migrate: operation not permitted
如何解决这个问题?
英文:
I'm following this Go + GraphQL tutorial https://www.howtographql.com/graphql-go/4-database/ and I got stuck at the point where I'm trying to install and then run migrations.
The entire command chain is
go get -u github.com/go-sql-driver/mysql
go build -tags 'mysql' -ldflags="-X main.Version=1.0.0" -o $GOPATH/bin/migrate github.com/golang-migrate/migrate/v4/cmd/migrate/
cd internal/pkg/db/migrations/
migrate create -ext sql -dir mysql -seq create_users_table
migrate create -ext sql -dir mysql -seq create_links_table
But specifically in
go build -tags 'mysql' -ldflags="-X main.Version=1.0.0" -o $GOPATH/bin/migrate github.com/golang-migrate/migrate/v4/cmd/migrate/
cd internal/pkg/db/migrations/
I will get the following error in my terminal:
go build github.com/golang-migrate/migrate/v4/cmd/migrate: copying /var/folders/f9/d6pn7fz92w53vcpywqd_08zm0000gp/T/go-build1656176552/b001/exe/a.out: open /bin/migrate: operation not permitted
How to solve this?
答案1
得分: 2
$GOPATH
未设置(这是可以的,go
将回退到默认值)。
这导致$GOPATH/bin/migrate
的值评估为/bin/migrate
,而不是预期的值,比如/home/you/go/bin/migrate
(其中/home/you/go
是默认的$GOPATH
)。
为了在$GOPATH
未设置的情况下使用默认值,你的go build
命令应该调用$(go env GOPATH)
,而不是直接使用$GOPATH
:
go build -tags 'mysql' -ldflags="-X main.Version=1.0.0" -o $(go env GOPATH)/bin/migrate github.com/golang-migrate/migrate/v4/cmd/migrate/
该教程错误地假设$GOPATH
环境变量总是被设置了。
英文:
$GOPATH
is not set (which is fine and go
will fallback to default value).
That causes $GOPATH/bin/migrate
evaluate to /bin/migrate
instead of its expected value - something like /home/you/go/bin/migrate
(where /home/you/go
is default $GOPATH
).
To use the default value in case $GOPATH
is not set; your go build
command should call $(go env GOPATH)
instead of using $GOPATH
directly:
go build -tags 'mysql' -ldflags="-X main.Version=1.0.0" -o $(go env GOPATH)/bin/migrate github.com/golang-migrate/migrate/v4/cmd/migrate/
That tutorial is simply making wrong assumption that $GOPATH
environment variable is always set.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论