/ informatics / language /

[edit]

Definition

Go is a compiled, statically typed programming language developed by Google.

// hello beer: drink 0.5 liter in 4 rounds
package main

import (
    "fmt"
    "time"
)

const LITER_PER_SIP = 37e-3

func sips2liter( num_sips int ) float64 {
    return float64( num_sips ) * LITER_PER_SIP
}

func main(){

    beer_l := 0.5
    rounds := 4
    sips_per_round := [] int {3, 7, 4, 2}

    fmt.Printf("I drink %g l in %d rounds:\n", beer_l, rounds)
    for _, sips := range sips_per_round {
        fmt.Printf("I drink %d sips.\n", sips)
        time.Sleep( time.Duration(sips) * time.Second)
        beer_l = beer_l - sips2liter( sips )
        if (beer_l <= 0.0){
            fmt.Println("My beer is empty!")
            break
        } else {
            fmt.Printf("I have %g l beer left.\n", beer_l)
        }
    }
}

Run your own program at the Go Playground!

Important Basics

Data Types

In Go you can define distinct new types such as type ipv4addr uint32

Collections

Control Flow

if answer != 42 {
    return "Wrong answer"
}

switch score {
    case 0, 1, 3:
        fmt.Println("Terrible")
    case 4, 5:
        fmt.Println("Awesome")
    default:
        fmt.Println("Unknown")
}
Example: Hello World

package main

import "fmt"

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

References