dongmuyuan3046 2016-09-21 17:34
浏览 3013

设置环境变量后os.GetEnv()不起作用

I have the case dosen't work function

os.GetEnv()

I have set a variable in my system ADDR="192.168.1.100" trought file .bashrc and .profile. So if I open terminal and type below command, I get good result

$ echo $ADDR

192.168.1.100

Why in below very simply program I get Error if variable is correct set in system ?

func main(){
    addr := os.Getenv("ADDR")
    if addr == "" {
        return errors.New("missing addres")
    }
}

I also restarted IDE a many times. Tried write in terminal again

$ env ADDR="192.168.1.100"

but still this same effect.

  • 写回答

4条回答 默认 最新

  • dsfe167834 2016-09-22 11:58
    关注

    I think the problem is likely that you are not exporting the variable, so the sub process (i.e. you ide, shell, is not getting it).

    ADDR="192.168.1.100" go run main.go
    

    or

    export ADD="192.168.1.100"
    go run main.go
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献