How to compile a program in Go Language?

It looks like you are following instructions from:

The Way to Go: A Thorough Introduction to the Go Programming Language By Ivo Balbaert. Section 2.3 Installing Go on a Linux system

These instructions are out of date. They use an obsolete release of Go, release 0.60. You have installed Go release 1.1.1.

For up-to-date instructions see Installing Go from source

Also, when you copy programs from the book, the book uses “ (left double quotation mark) and ” (right double quotation mark) in the code examples. Go expects " (quotation mark).

Write the test.go Go program as:

package main

func main() {
    println("Hello", "world")
}

When you installed Go, it told you it "Installed commands in /root/go/bin." You need to have /root/go/bin in your $PATH so that it can find (recognize) the Go commands.

From the directory which contains the test.go file, run

$ export PATH=$PATH:/root/go/bin
$ go version
go version go1.1.1 linux/amd64
$ go run test.go
Hello world

If this fails, what output do you get?


It looks like you've successfully installed Go from source, but you should really work your way through the Go Tour which will provide an introduction to the concepts of programming in Go.

The code you provided is missing a few sections. You need to import the "fmt" library, and then call any functions in it by prefacing them with fmt. .

For example:

package main

import "fmt"

func main() {
         fmt.Println(“Hello”, “world”)
}

I'd also recommend going through the links on this page in order. They gradually introduce more complex concepts as they go along.

Also, although using 6g is a valid way of compiling Go code, it's more usual to test code using go run, and to compile using go build. See http://golang.org/cmd/go/ for more info.

I hope that helps.

Tags:

Debian

Go