Installing Golang and Hello World in linux mint

This tutorial will work also to Ubuntu and similar linux distros.

Step 1 – Download

 

Since golang is having updates frequently, I recommend installing only from http://golang.org/doc/install, and not from apt-get, or any package other than the official site, because you will have an old release of golang tools.

Open http://golang.org/dl/ and download the lastest file to OS: linux.
Right now the last version is 1.3, so you must download go1.3.linux-amd64.tar.gz (for 64 bits computers (you probably have a 64 bits computer)).

Download the file with your browser (click here), or in Terminal:

cd ~/Downloads
wget http://golang.org/dl/go1.3.linux-amd64.tar.gz

Note: “cd ~” takes you to the user folder, example “cd ~” is equals to “cd /home/rover” in my machine.

 

Step 2 – Install

In a terminal, navigate to the download folder (where go1.3.linux-amd64.tar.gz is) and install it on /src/local folder:

sudo tar -C /usr/local -xzf go1.3.linux-amd64.tar.gz

Now you have to enable go as a program.

export PATH=$PATH:/usr/local/go/bin

Right now you can type “go version” in terminal to ensure go is installed successfully.

 

Step 3 – Hello World!

 

Create a hello.go file, having:

package main

import "fmt"

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

In terminal, go to the same folder you saved hello.go, and run on terminal:

go run hello.go

Voilá!

Step 4 – Golang external imports

 

Let’s create a more elaborated Hello World, using go-gtk lib.
In golang, we can import a git repository only declaring import “github.com/mattn/go-gtk/gtk” . But to have this working, we must configure $GOPATH, that must point to your project directory. Create a golang-hello folder, to hold the project, and create a “src”, “pkg” and “bin” folders. To configure $GOPATH, supposing that you created your project folder as “/home/rover/workspace/golang-hello”, use:

export GOPATH=/home/rover/workspace/golang-hello

In “src” folder, create a file called gtkhello.go, having the following code:

package main

import (
    "github.com/mattn/go-gtk/glib"
    "github.com/mattn/go-gtk/gtk"
)

func main() {
    gtk.Init(nil)
    window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
    window.SetPosition(gtk.WIN_POS_CENTER)
    window.SetTitle("Hello GTK+Go world!")
    window.SetIconName("gtk-dialog-info")
    window.Connect("destroy", func(ctx *glib.CallbackContext) {
        gtk.MainQuit()
    }, "foo")
    vbox := gtk.NewVBox(false, 1)
    button := gtk.NewButtonWithLabel("Hello world!")
    button.SetTooltipMarkup("Say Hello World to everybody!")
    button.Clicked(func() {
        messagedialog := gtk.NewMessageDialog(
            button.GetTopLevelAsWindow(),
            gtk.DIALOG_MODAL,
            gtk.MESSAGE_INFO,
            gtk.BUTTONS_OK,
            "Hello!")
        messagedialog.Response(func() {
            messagedialog.Destroy()
        })
        messagedialog.Run()
    })
    vbox.PackStart(button, false, false, 0)
    window.Add(vbox)
    window.SetSizeRequest(300, 50)
    window.ShowAll()
    gtk.Main()
}

Before run this program properly, you must install GTK developer packages. Follows:

sudo apt-get install libgtk2.0-dev libgtk-3-dev

Then run it, in terminal:

cd src/
go get
go run gtkhello.go

Cheers! 🙂

Deixe seu comentário