dqssst0144 2012-08-05 23:05
浏览 68
已采纳

如何配置Go,使其可以访问OSX中的环境变量

I am developing on OSX 10.7.4.

I have been experiencing some problems getting the "go get" command to work. In the course of trying to figure out what the problem was, I realized that Go was having trouble reading the $PATH environment variable.

The following script exhibits the problem:-

(PATH is returned as an empty string, and syscall.Getenv returns ok=false)

package main


import (
    "os"
    "syscall"
    "fmt"
)

func main() {

    path := os.Getenv("PATH")
    fmt.Println(path)

    syscall_path, ok := syscall.Getenv("PATH")
    fmt.Println(syscall_path)
    fmt.Println(ok)

}

I cannot imagine that this is a bug, as it is too dramatic to have gone unnoticed. So my question is this: In what way am I being stupid now?

:-)

I.e. What do I need to do to enable Go to access environment variables?

--Update:

As it turns out, it is actually a problem with my shell - Homebrew stopped working also, with the error message:

/usr/local/Library/Homebrew/global.rb:95: private method `split' called for nil:NilClass (NoMethodError)
    from /usr/local/bin/brew:10:in `require'
    from /usr/local/bin/brew:10

I think that the problem stemmed from my use of the fish shell, which does not seem to be setting up the environment as I might have expected.

  • 写回答

1条回答 默认 最新

  • douzhi1937 2012-08-06 15:35
    关注

    As it turns out, it was a malformed ~/.config/fish/config.fish file that was to blame.

    I was using:

    set -g VARNAME value
    

    Which instead should be:

    set VARNAME value
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?