如何在安装了 goclipse 的 eclipse 中运行 GO 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27477855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a GO project in eclipse with goclipse installed
提问by Tiina
I have installed goclipse in my eclipse and created a new go project.
我已经在我的 eclipse 中安装了 goclipse 并创建了一个新的 go 项目。
Now this is what I have:
现在这就是我所拥有的:
And I have hello.go looks like this:
我有 hello.go 看起来像这样:
package main
import "fmt"
func main() {
fmt.Println("Hello")
}
Then I press run button, or right click the file hello.go, and then run, it gives nothing. Besides, it is also empty in bin folder.
然后我按下运行按钮,或者右键单击文件 hello.go,然后运行,它什么也没给出。此外,它在 bin 文件夹中也是空的。
Then I press run configurations
button, almost empty there with only a project name. it gives:
然后我按下run configurations
按钮,那里几乎是空的,只有一个项目名称。它给:
Given Go package not found.
This is my Preferences for Go:
这是我对 Go 的偏好:
I noticed that both GOROOT and GOPATH are different from those listed in explorer, but once I changed them to C:\Go\src\pkg or C:\Go\src, the Apply and OK button becomes disabled. Besides, I don't have C:\Go\src\pkg folder either.
我注意到 GOROOT 和 GOPATH 与资源管理器中列出的不同,但是一旦我将它们更改为 C:\Go\src\pkg 或 C:\Go\src,Apply 和 OK 按钮将被禁用。此外,我也没有 C:\Go\src\pkg 文件夹。
I can go install
or go build
in command line, but I would like to use eclipse for another much larger project. It's windows 7, and the eclipse version is Luna Service Release 1 (4.4.1). Thank you.
我可以go install
或go build
在命令行中,但我想将 eclipse 用于另一个更大的项目。它是 windows 7,eclipse 版本是 Luna Service Release 1 (4.4.1)。谢谢你。
EDIT: Now I have the following configurations, and it works fine: Go is installed at c:\Go in Preferences: GOROOT:C:\Go, GOPATH: C:\Users\Tiina\go_workspace The rest in Preferences is filled in automatically. helloTest project is located at C:\Users\Tiina\go_workspace\src\helloTest hello.go is located at C:\Users\Tiina\go_workspace\src\helloTest\hello.go Maybe because I create a project from existing code, and there was something left behind, it automatically adds another GOPATH, in Preferences delete it.
编辑:现在我有以下配置,它工作正常:Go 安装在 c:\Go 中的 Preferences: GOROOT:C:\Go, GOPATH: C:\Users\Tiina\go_workspace Preferences 中的其余部分自动填充. helloTest 项目位于 C:\Users\Tiina\go_workspace\src\helloTest hello.go 位于 C:\Users\Tiina\go_workspace\src\helloTest\hello.go 也许是因为我从现有代码创建了一个项目,并且留下了一些东西,它会自动添加另一个 GOPATH,在 Preferences 中删除它。
采纳答案by VonC
If GOROOT
refers to where go is installed (C:\Go
), then you need to make sure that:
如果GOROOT
是指安装 go 的位置 ( C:\Go
),那么您需要确保:
GOPATH
differs fromGOROOT
(it is important, becauseGOROOT/[src|pkg|bin]
are folders for the Go language itself, not for your own sources)GOPPATH
points to a folder under which all your different Go project will reside (for instanceC:\Users\yourName\Go
: that defines a workspace)- your eclipse project is created in
GOPATH\src\myproject
GOPATH
不同于GOROOT
(这很重要,因为GOROOT/[src|pkg|bin]
是 Go 语言本身的文件夹,而不是您自己的源)GOPPATH
指向一个文件夹,您所有不同的 Go 项目都将在该文件夹下驻留(例如C:\Users\yourName\Go
:定义工作区)- 您的 Eclipse 项目是在
GOPATH\src\myproject
See "How to Write Go Code" to make sure that your installation and project sources respect the expected organization.
请参阅“如何编写 Go 代码”以确保您的安装和项目源符合预期的组织。
The OP Tiinareports in the comments:
Now it works, but I noticed two things odd.
- First what I did: I move
helloTest
folder intoC:\Users\Tiina\go_workspace\src
, so now it is atC:\Users\Tiina\go_workspace\src\helloTest
.- Then I create project from existing code. Nothing else changed. Now I have two
GOPATH
in explorer, one isC:\Users\Tiina\go_workspace\src
, the other one isC:\Users\Tiina\go_workspace\helloTest\src
.
The latter one did no exist at the beginning
现在它起作用了,但我注意到两件事很奇怪。
- 首先我做了什么:我将
helloTest
文件夹移动到C:\Users\Tiina\go_workspace\src
,所以现在它在C:\Users\Tiina\go_workspace\src\helloTest
.- 然后我从现有代码创建项目。其他什么都没有改变。现在我
GOPATH
在资源管理器中有两个,一个是C:\Users\Tiina\go_workspace\src
,另一个是C:\Users\Tiina\go_workspace\helloTest\src
。
后者一开始并不存在
I suspect what goclipsedoes is define one GOPATH
per project (or complete the existing GOPATH
).
If you create or import a project, it will define/complete GOPATH
in <that project/src>
If you hello.go is within that <that project/src/>
folder, then it should build and work as expected.
我怀疑goclipse所做的是为GOPATH
每个项目定义一个(或完成现有的GOPATH
)。
如果您创建或导入一个项目,它将GOPATH
在<that project/src>
hello.go 中定义/完成如果您在该<that project/src/>
文件夹中,那么它应该按预期构建和工作。
The user guide "project structure" of goclipsementions:
The project location is not part of any
GOPATH
entry.
In this case the project location will implicitly be added as an entry to theGOPATH
, and a Go workspace structure with thebin
,pkg
, andsrc
directories will be used in the project.Note that the project's implicit
GOPATH
entry will only apply to the source modules in that project. It will not be visible to other Goclipse projects (unless the entry is explicitly added to the globalGOPATH
).In the
src
folder you can create Go source files that will be compiled into a library package (and placed intopkg
), or into an executable (and placed inbin
)
项目位置不是任何
GOPATH
条目的一部分。
在这种情况下,项目位置将隐式添加为一个条目的GOPATH
,和去工作空间结构的bin
,pkg
和src
目录将在项目中使用。请注意,项目的隐式
GOPATH
条目仅适用于该项目中的源模块。它对其他 Goclipse 项目不可见(除非该条目明确添加到 globalGOPATH
)。在该
src
文件夹中,您可以创建 Go 源文件,这些文件将被编译为库包(并放入pkg
)或可执行文件(并放入bin
)
回答by Vishal
Here's what one needs to do. One must always follow GO's convention of the directory structure. In eclipse, once a new project is created just create a "new folder" under the src directory by right clicking on the src folder. And now underneath this folder create a new GO file. I had issues running this on my MAC but by following the above steps was able to resolve.
这是一个人需要做的。必须始终遵循 GO 的目录结构约定。在 Eclipse 中,一旦创建了一个新项目,只需通过右键单击 src 文件夹在 src 目录下创建一个“新文件夹”。现在在这个文件夹下创建一个新的 GO 文件。我在我的 MAC 上运行它时遇到了问题,但按照上述步骤可以解决。
- Vishal (www.vishalpandya.com)
- Vishal (www.vishalpandya.com)
回答by olippuner
Please respect the fact, that if you change GOPATH while working in Eclise/GoClipse, you will have to Quit/restart Eclipse in order to Eclipse have the new environment variables to be reread.
请尊重这样一个事实,如果您在 Eclise/GoClipse 中工作时更改 GOPATH,您将必须退出/重新启动 Eclipse,以便 Eclipse 重新读取新的环境变量。