Golang – 在SublimeText2中找不到包

huangapple go评论95阅读模式
英文:

Golang - cannot find package in SublimeText2

问题

当我尝试在SublimeText2中执行我的.go脚本时,使用我的包newmath,我得到以下错误。

"test.go:5:2: import "newmath": cannot find package"

这是我在SublimeText2中的构建系统。

{
"cmd": ["go","run", "${file}"],
"working_dir": "${file_path}",
"selector": "source.go, source.g"
}

但是当我在终端中执行我的脚本时,go编译器可以找到该包,并且成功执行。我注意到,GOPATH在重新启动或一段时间后会自动重置。

我的Go环境:

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/daggi/gocode/"
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CGO_ENABLED="1"

我认为这是SublimeText2的路径/环境问题,但我不确定。

谢谢。

英文:

when i try to execute my .go script with my package newmath i get following error in SublimeText2.

"test.go:5:2: import "newmath": cannot find package"

Thats my build-system in SublimeText2

{
"cmd": ["go","run", "${file}"],
"working_dir": "${file_path}",
"selector": "source.go, source.g"
}

but when i execute my script in the terminal the go-compiler can find the package and it is succesfully executed. And i have noticed that the GOPATH is reseted after reboot or after a while by itself :/

My Go env:

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/daggi/gocode/"
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CGO_ENABLED="1"

I think it is a path/environment problem of SublimeText2 but i dont know.

Thank you.

答案1

得分: 2

{
"cmd": ["go","run", "${file}"],
"working_dir": "${file_path}",
"selector": "source.go, source.g",
"path": "/usr/lib/go/bin:$PATH",
"env": {
"GOPATH": "$HOME/gocode/",
"GOROOT": "/usr/lib/go",
"PATH": "/usr/lib/go/bin:$PATH"
}
}

英文:

It seems the build system does not share the GoSublime settings env, nor your own shell env. So you have to set it up specifically:

Build systems

{
	"cmd": ["go","run", "${file}"],
	"working_dir": "${file_path}",
	"selector": "source.go, source.g",
	"path": "/usr/lib/go/bin:$PATH",
	"env": {
		"GOPATH": "$HOME/gocode/",
		"GOROOT": "/usr/lib/go",
		"PATH": "/usr/lib/go/bin:$PATH"
	}
}

You could also switch your GOPATH setting to use ${project} instead of that explicit GOPATH, if you work in a way that your Sublime project is your GOPATH.

答案2

得分: 1

可能与这个GoSublime问题有关,尽管您似乎没有使用GoSublime:

> 这在OS X和Ubuntu上已经报告过了。问题似乎是shell变量没有传递给GUI(在这种情况下是ST2),这超出了GoSublime的范围。

https://github.com/DisposaBoy/GoSublime/issues/71

在您的Sublime Text 2用户设置中,您可以尝试设置您的环境变量(我说这个并不确定"env"条目是否被GoSublime或ST2专门读取,所以这可能有效也可能无效,如果无效的话,您可以安装并使用GoSublime,我个人认为它非常棒):

  "env": {
    "GOROOT": "/usr/lib/go",
    "GOPATH": "/home/daggi/gocode"
  }

希望对您有帮助。

英文:

Possibly related to this GoSublime issue, even though you don't seem to use GoSublime :

> This has been reported on OS X and Ubuntu. The problem appears to be that the shell variables are not passed passed to GUIs(ST2 in this case) which is beyond GoSublime.

https://github.com/DisposaBoy/GoSublime/issues/71

In your Sublime Text 2 User Settings, you may try to set your environment variables (I say this not knowing for sure if the "env" entry is specifically read by GoSublime or ST2, so this may or may not work, if it doesn't you may want to install and use GoSublime, which is pretty awesome IMO):

  "env": {
    "GOROOT": "/usr/lib/go",
    "GOPATH": "/home/daggi/gocode"
  }

HTH

huangapple
  • 本文由 发表于 2012年12月12日 07:40:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/13830377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定