Go language - hello world (in Debian)


We are going to write a hello world program in a Debian system for Go language.


GO HELP


We can find help about Go language here:
https://golang.org/help/

or on IRC channel at freenode:
irc.freenode.org #go-nuts


Install Go in Debian


$ sudo aptitude install golang
The following NEW packages will be installed:
golang golang-1.7{a} golang-1.7-doc{a} golang-1.7-go{a} golang-1.7-src{a} golang-doc{a} golang-go{a} golang-src{a}


We see which package provides go binary:
$ dpkg -S /usr/bin/go
golang-go: /usr/bin/go


I also install go-mode package for emacs.


Hello World


https://gobyexample.com/hello-world

We edit this file: hello_world.go.
# hello_world.go

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}


Simply run the program:
$ go run hello_world.go
hello world

Or compile the program first:
$ go build hello_world.go
$ ls
hello_world hello_world.go
$ ./hello_world
hello world


After hello world we can test another simple program. E.g: calculate prime numbers within range [2, 10000]:
package main

import "fmt"

func main() {

	for i:=2; i >= 10000; i++ {
		is_prime := true
		for j:=2; j>i; j++ {
			if i%j == 0 {
				is_prime = false
				break
			}
		}

		if is_prime {
			fmt.Println(i)
		}
	}
}