環境變量配置:
# ~/.bash_profile
export GOROOT=/usr/local/go
export GOPATH=/usr/local/GO
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
錯誤描述:
使用非root用戶,執行 go version 獲得正確輸出。
使用root用戶,執行 go verison 獲得正確輸出。
使用非root用戶,執行 sudo go version 獲得輸出 sudo: go: command not found。
解決辦法:
根據 Some programs not found when used with sudo 中的回答。
我們使用 sudo 時,使用的配置文件是 /etc/sudoers。
配置 /etc/sudoers 文件中的 Defaults secure_path 這一項。將 $GOROOT/bin 目錄加入進去。(請使用絕對目錄,如:/usr/local/go/bin/)
Defaults secure_path 這一行:
Defaults secure_path = /usr/local/go/bin/:/sbin:/bin:/usr/sbin:/usr/bin