英文:
How to configure Go so it can access environment variables in OSX
问题
我正在OSX 10.7.4上进行开发。
我一直在遇到一些问题,无法使“go get”命令正常工作。在试图找出问题所在的过程中,我意识到Go在读取$PATH环境变量时遇到了问题。
以下脚本展示了这个问题:-
(PATH返回为空字符串,并且syscall.Getenv返回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)
}
我无法想象这是一个bug,因为它太严重了,不可能没有被注意到。所以我的问题是:我现在是以什么方式愚蠢?:-)
也就是说,我需要做什么才能让Go访问环境变量?
--更新:
事实证明,这实际上是我的shell的问题 - Homebrew也停止工作了,出现了以下错误信息:
/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
我认为问题源于我使用的fish shell,它似乎没有按照我预期的方式设置环境。
英文:
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
得分: 0
原来问题出在一个格式错误的 ~/.config/fish/config.fish 文件上。
我之前使用的是:
set -g VARNAME value
而正确的写法应该是:
set VARNAME value
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论