Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GoClipse - Setup tutorial - Version 2

GoClipse - Setup tutorial - Version 2

This tutorial schematically describes a possible solution for setting up Go, GoClipse (current version: 0.9.1) and a few related tools.

Gianluca Costa

November 28, 2014
Tweet

More Decks by Gianluca Costa

Other Decks in Programming

Transcript

  1. Brief introduction to GoClipse • Very nicely integrated within the

    Eclipse IDE • Can automatically build projects whenever a file is saved, showing errors in the editor and in the Problems window • Supports Go projects whose source files are in directories not listed in the GOPATH (see later)
  2. Brief introduction to GoClipse (2) • Employs Gocode to provide

    very fast code completion (e.g., when you press . Or Ctrl+Space while writing an identifier) • Employs Go oracle to support enhanced source code navigation: pressing F3 or clicking on a name while pressing Ctrl works like a hyperlink to its definition, even for standard library modules
  3. About this tutorial • This tutorial schematically describes a possible

    solution for setting up Go, GoClipse (current version: 0.9.1) and a few related tools • We'll take into account 2 similar paths: – A full-fledged installation, with a few optional components – A minimal setup, which can be handy if you need a standalone copy of GoClipse alongside another version of Eclipse • Feel free to experiment! ^__^
  4. Full installation 1)Install Go and set up your GOROOT 2)Set

    up your GOPATH 3)Install Git and the latest version of Gocode, for faster autocompletion (optional) 4)Install Mercurial and the latest version of Go oracle, for accurate symbol browsing (optional) 5)Install any edition of Eclipse - or reuse an installation that you already administer 6)Install GoClipse 7)Configure GoClipse
  5. Minimal installation 1)Install Go only if it's not on your

    machine, but always set up your GOROOT 2)Set up your GOPATH 3)Download just the Eclipse Platform - only if you can't reuse an existing Eclipse installation 4)Install GoClipse 5)Configure GoClipse
  6. Setting environment variables • If you are on a BASH-based

    Unix, environment variables can be set by editing the .profile configuration file (or .bashrc – it depends on your operating system) in your home directory. For example, you could add the following line: export MY_ENV_VAR=$HOME/MyDir • If you are on a recent version of Windows, right-click on the This PC icon on the Desktop, choose Properties, then Advanced system settings, then Environment variables
  7. Install Go and set up your GOROOT • Download Go

    from https://golang.org/ and install it: the specific procedure depends on your OS • Go might already be available on your system! Use which go or where go to find out! • Set the env. var. GOROOT to the absolute path of Go's installation directory (e.g., /opt/go on a Unix system, or C:\Go on Windows) • Add the GOROOT/bin subdirectory to your PATH environment variable
  8. Set up your GOPATH • Create a directory dedicated to

    your GoPath, for example /home/myself/gopath or C:\GoPath • Set up a global environment variable called GOPATH, pointing to that directory • Add GOPATH/bin to your PATH, although such directory doesn't exist yet • GOPATH can be not only a single directory, but a list of directories: for further info, please refer to:https://golang.org/doc/code.html
  9. Install Git • Git seems required in order to automatically

    download Gocode via “go get” • Most Linux distributions have the related installation packages in their repositories, and Git's website provides several binaries • Official website: http://git-scm.com/
  10. Install Gocode • From the command line, run: go get

    -u github.com/nsf/gocode (on Unix) go get -u -ldflags -H=windowsgui github.com/nsf/gocode (on Windows) • The program might take a few minutes and will not produce output in case of success
  11. Install Mercurial • Mercurial seems required in order to download

    Go oracle via “go get” • Just like Git, it is already in the software repositories of most Linux distributions, and its website provides many binary packages • Official website: http://mercurial.selenic.com/
  12. Install Go oracle • From the command line, execute: go

    get code.google.com/p/go.tools/cmd/oracle • This might take a few minutes and will not output anything in case of success
  13. Install Eclipse • Multiple installations of Eclipse can coexist on

    the same system – the only caveat usually consisting in keeping their respective workspaces separated • If you want to install a minimal Eclipse version dedicated to GoClipse, you might want to download just the Eclipse Platform, currently available at: http://download.eclipse.org/eclipse/downloads/ drops4/R-4.4.1- 201409250400/#PlatformRuntime
  14. Install GoClipse • GoClipse can be installed like most Eclipse

    plugins • For detailed installation instructions, please refer to: https://github.com/GoClipse/goclipse/blob/late st/documentation/Installation.md#installation • A basic version of CDT (C/C++ Development Tooling) might be automatically installed as well
  15. The workspace and the GOPATH • First of all, if

    you are setting up an Eclipse version dedicated to GoClipse, you'll probably want to keep the workspace separated from other Eclipse workspaces, to prevent conflicts • The workspace should reside in an arbitrary directory outside GOPATH • Your source code can be located either below a per-project src folder or below an src folder of your GOPATH: for the sake of simplicity, we'll focus on the first case
  16. Configure GoClipse 1)Before configuring GoClipse, choose your workspace, because settings

    are saved in the workspace itself 2)Within Eclipse, click on the “Window ==> Preferences...” menu item 3)Select the “Go” section
  17. Configure GoClipse (2) - General • The values of GOROOT

    and GOPATH should match the values of the same environment variables previously defined • Choose a value for GOOS and GOARCH, according to your current development platform. This seems useful for source code navigation • Check the remaining executable paths
  18. Configure GoClipse (3) - Tools • Open the Tools subsection

    • Click the Browse... buttons to select the actual path of your oracle and gocode executable files (to find them easily, consider that they should reside in GOPATH/bin) • You might want to disable the Enable Gocode log console checkbox • Confirm the dialog and restart Eclipse
  19. Creating a Go project • When creating a new Go

    project, select the option Create new project in workspace: while you work on that project, GoClipse will automatically prepend the project's directory to your GOPATH • On the other hand, by choosing Create project from existing source, you could work on an arbitrary source dir of your GOPATH
  20. Create a main file 1)Right-click on the project's src folder

    2)Choose New=>Folder 3)Call it, for example, MyProgram 4)Right-click on it and choose New=>Go File 5)In the dialog, select Command Source File, then Empty Main Function, and call it, for example, main.go 6)Write your source file (see next slide) and save it: GoClipse should already build the program for you
  21. A minimal source file package main import "fmt" func main()

    { fmt.Println("Hello, world! ^__^") }
  22. Build artifacts • Your PROJECT_DIR/bin directory will contain a MyProgram

    executable, where MyProgram is the name of the directory containing the main package • Additionally, every custom Go package within your project will create a related .a library file in the PROJECT_DIR/pkg/GOOS_GOARCH directory tree • If source files are decoupled from the project, the related GOPATH is used in lieu of PROJECT_DIR