打开/bin/migrate:在构建Go标签时不允许的操作。

huangapple go评论86阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年6月1日 08:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/72455304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定