如何在安装了goclipse的eclipse中运行GO项目?

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

How to run a GO project in eclipse with goclipse installed

问题

我在我的eclipse中安装了goclipse并创建了一个新的go项目。

现在我有以下内容:

如何在安装了goclipse的eclipse中运行GO项目?

我有一个名为hello.go的文件,内容如下:

package main

import "fmt"

func main() {
     fmt.Println("Hello")
}

然后我点击运行按钮,或者右键点击hello.go文件,然后选择运行,但是没有任何输出。此外,bin文件夹中也是空的。

然后我点击run configurations按钮,里面几乎是空的,只有一个项目名称。它给出了以下错误信息:

Given Go package not found.

这是我的Go首选项设置:
如何在安装了goclipse的eclipse中运行GO项目?

我注意到GOROOT和GOPATH与资源管理器中列出的不同,但是一旦我将它们更改为C:\Go\src\pkg或C:\Go\src,应用和确定按钮就变为禁用状态。此外,我也没有C:\Go\src\pkg文件夹。

我可以在命令行中使用go installgo build命令,但是我想在eclipse中使用它进行另一个更大的项目。我的操作系统是Windows 7,eclipse版本是Luna Service Release 1 (4.4.1)。谢谢。

编辑:现在我有以下配置,它可以正常工作:
Go安装在c:\Go
在首选项中:GOROOT:C:\Go, GOPATH: C:\Users\Tiina\go_workspace
其他首选项会自动填充。
helloTest项目位于C:\Users\Tiina\go_workspace\src\helloTest
hello.go文件位于C:\Users\Tiina\go_workspace\src\helloTest\hello.go
可能是因为我从现有代码创建了一个项目,留下了一些东西,它自动添加了另一个GOPATH,在首选项中删除它。

英文:

I have installed goclipse in my eclipse and created a new go project.

Now this is what I have:

如何在安装了goclipse的eclipse中运行GO项目?

And I have hello.go looks like this:

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.

Then I press run configurations button, almost empty there with only a project name. it gives:

Given Go package not found.

This is my Preferences for Go:
如何在安装了goclipse的eclipse中运行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.

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.

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.

答案1

得分: 14

如果GOROOT指的是Go的安装位置(C:\Go),那么你需要确保:

  • GOPATHGOROOT不同(这很重要,因为GOROOT/[src|pkg|bin]是Go语言本身的文件夹,而不是你自己的源代码)
  • GOPATH指向一个文件夹,其中包含你所有不同的Go项目(例如C:\Users\yourName\Go:这定义了一个**工作区**)
  • 你的Eclipse项目创建在GOPATH\src\myproject

请参考“如何编写Go代码”确保你的安装和项目源代码符合预期的组织结构。


OP Tiina评论中提到:

> 现在它可以工作了,但我注意到两件奇怪的事情。

> - 首先我做了什么:我将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
    后者在开始时不存在。

我怀疑goclipse是根据每个项目定义一个GOPATH(或者完善现有的GOPATH)。
如果你创建或导入一个项目,它将在<that project/src>中定义/完善GOPATH
如果你的hello.go文件在<that project/src/>文件夹中,那么它应该按预期进行构建和工作。

goclipse的用户指南“项目结构”提到:

> 项目位置不是任何GOPATH条目的一部分。
在这种情况下,项目位置将隐式添加为GOPATH的一个条目,并且在项目中使用binpkgsrc目录的Go工作区结构。

> 请注意,项目的隐式GOPATH条目仅适用于该项目中的源模块。它对其他Goclipse项目不可见(除非将该条目显式添加到全局GOPATH)。

> 在src文件夹中,你可以创建将编译为库包(并放置在pkg中)或可执行文件(并放置在bin中)的Go源文件。

英文:

If GOROOT refers to where go is installed (C:\Go), then you need to make sure that:

  • GOPATH differs from GOROOT (it is important, because GOROOT/[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 instance C:\Users\yourName\Go: that defines a workspace)
  • your eclipse project is created in GOPATH\src\myproject

See "How to Write Go Code" to make sure that your installation and project sources respect the expected organization.


The OP Tiina reports in the comments:

> Now it works, but I noticed two things odd.

> - First what I did: I move helloTest folder into C:\Users\Tiina\go_workspace\src, so now it is at C:\Users\Tiina\go_workspace\src\helloTest.

  • Then I create project from existing code. Nothing else changed. Now I have two GOPATH in explorer, one is C:\Users\Tiina\go_workspace\src, the other one is C:\Users\Tiina\go_workspace\helloTest\src.
    The latter one did no exist at the beginning

I suspect what goclipse does is define one GOPATH per project (or complete the existing GOPATH).
If you create or import a project, it will define/complete GOPATH in &lt;that project/src&gt;
If you hello.go is within that &lt;that project/src/&gt; folder, then it should build and work as expected.

The user guide "project structure" of goclipse mentions:

> The project location is not part of any GOPATH entry.
In this case the project location will implicitly be added as an entry to the GOPATH, and a Go workspace structure with the bin, pkg, and src 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 global GOPATH).

> In the src folder you can create Go source files that will be compiled into a library package (and placed into pkg), or into an executable (and placed in bin)

答案2

得分: 1

以下是需要做的事情。必须始终遵循GO的目录结构约定。在eclipse中,创建一个新项目后,只需右键单击src文件夹,在src文件夹下创建一个“新文件夹”。然后,在这个文件夹下创建一个新的GO文件。我在我的MAC上运行时遇到了问题,但通过按照上述步骤解决了。

  • Vishal(www.vishalpandya.com)
英文:

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.

答案3

得分: 0

请注意,如果您在Eclipse/GoClipse中更改了GOPATH,请退出/重新启动Eclipse,以便Eclipse能够重新读取新的环境变量。

英文:

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.

huangapple
  • 本文由 发表于 2014年12月15日 13:24:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/27477855.html
匿名

发表评论

匿名网友

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

确定