How to run a GO project in eclipse with goclipse installed
Asked Answered
D

3

16

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

Now this is what I have:

enter image description here

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: enter image description here

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.

Deanery answered 15/12, 2014 at 5:24 Comment(3)
Is hello.go within on c:\Go\src? Where Go itself is installed?Amuck
@Amuck Hi, Go is installed right under C:\ with a path C:\Go, and hello.go is created in the explorer by adding new files, i.e, right click on the project name and then add->file. So now I believe hello.go is in the project root (C:\Users\Tiina\go_workspace\helloTest). I also tried it with hello.go in c:\Go\src or /helloTest/srcDeanery
GOPATH should not point to a diretory inside a Go folder.Francophobe
A
14

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 <that project/src>
If you hello.go is within that <that project/src/> 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)

Amuck answered 15/12, 2014 at 7:6 Comment(6)
Now, in preference, I have GOROOT:C\Go, GOPATH: C:\Users\Tiina\go_workspace, which is also the workspace for eclipse. In helloTest project in explorer, I have GOROOT - C:\Go\src\pkg and GOPATH - C:\Users\Tiina\go_workspace\src. It automatically adds \src\pkg and \src, respectively. In fact, I don't have either of these two folders. When I run it, still "Given Go package not found."Deanery
@Deanery I would really recommend keeping your projects separate from Eclipse workspace. But it should work nonetheless. What the exact full path of your hello.go in that new configuration?Amuck
Go itself - c:\Go; helloTest project: C:\Users\Tiina\go_workspace\helloTest; hello.go: C:\Users\Tiina\go_workspace\helloTest\hello.go; GOROOT in explorer: C:\Go\src\pkg; GOPATH in explorer: C:\Users\Tiina\go_workspace\src; GOROOT in preferences: c:\Go; GOPATH in preferences: C:\Users\cmcc\java_go_workspace;C:\Users\Tiina\go_workspace\helloTestDeanery
@Deanery I can assure you, within a Go workspace (here GOPATH), your sources should be in GOPATH\src, which means hello.go should be in C:\Users\Tiina\go_workspace\src\helloTest\hello.go.as described in golang.org/doc/code.html. Make sure your eclipse project is created in GOPATH\src, and not in the default eclipse workspace (which has nothing to do with a Go workspace described in golang.org/doc/code.html).Amuck
Hi, 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.Deanery
@Deanery Ok, I have included your comment in the answer for more visibility, as well as a opinion about that working configuration.Amuck
Y
1

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.

  • Vishal (www.vishalpandya.com)
Yarkand answered 19/9, 2017 at 8:17 Comment(0)
R
0

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.

Realtor answered 17/11, 2019 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.