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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法