Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How to correctly install and configure the Go Language in Ubuntu. There are many packages to choose from but which packages do I need to install and what do I need to configure afterwards to be able to use any of the Go packages without having a "cannot find package" error for example or any other basic errors of that kind.

I installed the golang package but do I need to install any additional ones or configure something else?

As an example try to run the following:

package main

import (
        "http"
        "log"
)

func HelloServer(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "text/plain")
    w.Header().Set("Connection", "keep-alive")
        w.Write([]byte("hello, world!\n"))
}
func main() {
        http.HandleFunc("/", HelloServer)
        log.Println("Serving at http://127.0.0.1:8080/")
        http.ListenAndServe(":8080", nil)
}
share|improve this question

2 Answers 2

up vote 6 down vote accepted

Installing the golang metapackage should be enough:

sudo apt-get install golang

"This package is a metapackage that, when installed, guarantees that (most of) a full Go development environment is installed." So, after that all you need is type go help for basic commands:

Go is a tool for managing Go source code.

Usage:

go command [arguments]

The commands are:

build       compile packages and dependencies
clean       remove object files
env         print Go environment information
fix         run go tool fix on packages
fmt         run gofmt on package sources
get         download and install packages and dependencies
install     compile and install packages and dependencies
list        list packages
run         compile and run Go program
test        test packages
tool        run specified go tool
version     print Go version
vet         run go tool vet on packages

Make a hello world in gedit. Example from their website:

package main

import "fmt"

func main() {
    fmt.Println("Hello world\n")
}

(save it as hello.go)

Executing ...

 go run hello.go

yields ...

 Hello world


gorun lets you use a she-bang. Do read this topic though. Above example can then be:

#!/usr/bin/gorun    
package main

func main() {
    println("Hello world!\n")
}

And making it executable:

chmod +x hello.go
./hello.go

yields ...

Hello world!

(I added the \n myself)


Your example has a mistake:

import http needs to be net/http

go run test.go
2014/05/10 20:15:00 Serving at http://127.0.0.1:8080/
share|improve this answer

I have been using Golang for 2 week now and I want ot share how you can install the latest Go version (v1.3.1) on Ubuntu 13.x / 14.x.

Go V1.3

Default folder: /usr/lib/go

cd /usr/lib/
apt-get install mercurial
hg clone -u release https://code.google.com/p/go
cd /usr/lib/go/src
./all.bash

Configure environment variables

ll /usr/lib/go
nano ~/.bashrc

# append this to your script
export GOPATH=/srv/go
if [ -d "/usr/lib/go/bin" ] ; then
    PATH="${GOPATH}/bin:/usr/lib/go/bin:${PATH}"
fi

[Update the GO Version via Version Control later if needed]

cd /usr/lib/go
hg update release

!!! Reconnect the SSH Terminal to execute the new .bashrc

Check the environment settings

go env

Create my dev environment. It can be anything so also ~/go/ if you want.

mkdir -p /srv/go
cd    /srv/go/
mkdir -p $GOPATH/src/github.com/username

Testing

mkdir -p $GOPATH/src/github.com/username/hello
cd    $GOPATH/src/github.com/username/hello
nano hello.go

package main
import "fmt"
func main() {
    fmt.Printf("goeiedag, wereld\n")
}

Run it

go run hello.go

Build the binary and install it in $GOPATH/bin/

cd $GOPATH/src/github.com/username/hello
go install
ll $GOPATH/bin/
hello
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.