解决 gomobile 安装失败出现‘open /usr/local/go/bin/gomobile: permission denied’

457 阅读1分钟

项目需求需要编一个开源库so,打开项目一看用的是go开发的,小ks安装下go环境就是。打开go官网GO Insatall下载安装,一切的都很顺利完成,由于需要编译so还需要用到 gomobile,兴高采烈的打开gomobile安装官网执行命令安装

$ go install golang.org/x/mobile/cmd/gomobile@latest
$ gomobile init

这时问题来了,包错怎么也安装不上没有权限?

$ go install golang.org/x/mobile/cmd/gomobile@latest

golang.org/x/mobile/cmd/gomobile: go install golang.org/x/mobile/cmd/gomobile: copying /var/folders/sr/4z4j7gp161998_wmzq4pxsbw0000gn/T/go-build1239366083/b001/exe/a.out: open /usr/local/go/bin/gomobile: permission denied

于是加上 sudo 再次安装

$ sudo go install golang.org/x/mobile/cmd/gomobile@latest

等待安装,但是终端什么也没有给我反馈,也没有报错?也没有安装上gomobile,不解???怎么回事小老弟

查看配置的环境变量,配置了GOBIN

export GOROOT=/usr/local/go
export GOPATH=/Users/user/go
export GOBIN=$GOROOT/bin
export PATH=$PATH:$GOBIN:$GOPATH/bin

删除GOBIN 环境变量

export GOPATH=/Users/user/go
export PATH=$PATH:$GOPATH/bin

再次执行还是报同样的错误,于是查看go env,GOBIND有值

% go env
....

GOBIN='/usr/local/go/bin'
....

GOMODCACHE='/Users/user/go/pkg/mod'

....

删掉GOBIN

go env -w GOBIN=""

再次执行gomobile 安装命令,咦!没有报错了成功安装了!!!完美收官

$ gomobile init
$ gomobile
Gomobile is a tool for building and running mobile apps written in Go.
To install:
$ go install golang.org/x/mobile/cmd/gomobile@latest

$ gomobile init

At least Go 1.16 is required.

For detailed instructions, see https://golang.org/wiki/Mobile.

Usage:

gomobile command [arguments]

Commands:

bind        build a library for Android and iOS

build       compile android APK and iOS app

clean       remove object files and cached gomobile files

init        build OpenAL for Android

install     compile android APK and install on device

version     print version

Use 'gomobile help [command]' for more information about that command.

给权限安装(记录下)

$ sudo chmod a+w /usr/local/go/bin/

d6f2bc0be49dee93f8cad625ec2359e5.jpeg