Go Env - Go环境配置问题

AI 摘要: 本文主要描述了作者遇到的远程开发机的Golang环境问题,并分析了问题的原因和解决方法。文章提到了环境说明和Linux配置加载文件的回顾,以及Golang环境配置的常见方式。最后,作者总结了两个经验教训。

1. 问题现象

今天遇到远程开发机的 Golang 环境问题

2. 分析过程

2.1. 环境说明

因为司内存在网络安全策略,**开发域是无法直接连接测试网域**或**生产网域**下的机器,需要通过**跳板机**器操作,为了加速本地 Coding+Debug 效率,在测试域的机器上打了**热更新的transform进程,** 用于与测试环境的热更新处理;

简要说下我的开发模式

development_mode

2.2. 回顾下 Linux 配置加载文件

  • 取得 bash 时需要完整的登陆流程的,就称为 login shell
    • 读取路径:/etc/profile → ~/.bash_profile → ~/.bashrc
  • 取得 bash 接口的方法不需要重复登陆的(比如你在原本的 bash 环境下再次下达 bash 这个命令) : ~/.bashrc

2.3. Golang 环境配置

常规情况下,我们一般习惯会在上述 Linux 的相关配置路径加入 Go Env 的相关配置,比如我这里把下面的 GOPATH 等环境配置放置在~/.my.sh 下,然后在~/.bashrc 中通过souce ~/.my.sh 包含

gopath

我的环境现象就是正常 Login Shell 后,go env 输出的GOPROXY正常,但通过 ssh remote shell 会出现问题

1
2
3
4
5
6
7
8
# 正常Login Shell情况没有问题,GOPROXY正常,没有https://前缀
[luping@container ~] {15:38:26} (0)
$ go env|grep GOPROXY
GOPROXY="goproxy.woa.com,direct"

# 但通过remote 出来的go
$ssh luping@9.135.17.5 -p 36000 'go env'
GOPROXY="https://goproxy.woa.com,direct"

使用 root 账号打印 GOPROXY,可以断定与 linux 账号本地环境相关

1
2
3
# 更换root账号打印,发现也是正常
[root@container ~]# go env|grep GOPROXY
GOPROXY="goproxy.woa.com,direct

2.4. bash_it 说明

bash_it 是一个 cmd shell 提示工具,~/bash_it 初始化后会在~/.bashrc 中增加很多 shell 脚本内容,其中一部分就是如果非交互环境,则不会运行

1
2
3
4
5
# If not running interactively, don't do anything
case $- in
  *i*) ;;
    *) return;;
esac

2.5. $- 参数说明

继续分析,因为 bash shell 环境使用了 ~/bash_it ,会在 ~/.bashrc 加入下面内容,这个 $- 是一个 shell varibales 的属性和参数配置:

  • ssh 如果按正常情况下
1
2
3
4
5
6
7
  # 登录后输出 $-
  $ echo $-
  himBH

  # 通过remote ssh 结果
  $ ssh luping@9.135.17.5 -p 36000 'echo $-'
  hBc

To understand the output of echo $- you need to look up the options in your shell’s manual. For example, in Bash, echo $- outputs himBHs for me, which means that the -h, -m, -B and -H options are enabled (see help set for details), that the shell is interactive (-i) and reading from standard input (-s).

参考:

2.6. 原因 1: 加载 export GOPROXY 环境配置位置问题

错误的 source ~/.my.sh 位置,我再开发机上将相关的本地 Shell 配置都放入到 ~/.my.sh 中,然后在 ~/.bashrc 文件的最后加入。

因为 bash_it$- 环境有做检测,即非交互环境会直接 return 返回,这样就会导致放置在最后的 source ~/.my.sh 中的 GOPORXY 等环境变量无法被重置;反之若直接登录的话,因为是交互 Shell 环境,则没有此问题!

2.7. 原因 2: 再看 Golang 环境变量

  1. 可以通过 go env -w <NAME>=<VALUE> 配置 Go 环境变量,具体存储位置为 os.UserConfigDir
  2. 该位置可以通过 go env GOENV 打印出来
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$ go help environment
The go command and the tools it invokes consult environment variables
for configuration. If an environment variable is unset, the go command
uses a sensible default setting. To see the effective setting of the
variable <NAME>, run 'go env <NAME>'. To change the default setting,
run 'go env -w <NAME>=<VALUE>'. Defaults changed using 'go env -w'
are recorded in a Go environment configuration file stored in the
per-user configuration directory, as reported by os.UserConfigDir.
The location of the configuration file can be changed by setting
the environment variable GOENV, and 'go env GOENV' prints the
effective location, but 'go env -w' cannot change the default location.
See 'go help env' for details.

$ go env GOENV
/data/home/luping/.config/go/env

通过查看 env 文件配置,就可以看到之前错误配置的 GOPROXY 了,将其修改后就正常!

3. 小结

  1. bash_it~/.bashrc 有改动,如果涉及非交互的 Shell 则有可能遇到加载顺序问题
  2. Golang 的环境默认配置存储在~/.config/go/env 修改,登录后可以被 ~/.bashrc 配置的属性覆盖