Basic features of Go

Hello World package main import "fmt" func main () { fmt . Println ( "Hello, 世界" ) }

Declarations var a int var b bool a = 15 b = false a := 15 b := false

Functions func hello ( who string ) string { return fmt . Sprintf ( "Hello %s" , who ) } func fibo ( a , b int ) ( int , int ) { a , b = a + b , a return a , b } incr := func ( n int ) int { return n + 1 }

Control statements if x > y { return x } else { return y } const nb = 10 for i := 0 ; i < nb ; i ++ { fmt . Printf ( "Iteration n°%d

" , i ) }

Array / Slice var array [ 10 ] int var slice [] int list := [] string { "a" , "b" , "c" , "d" , "e" , "f" } other := append ( list [ 0 : 2 ], list [ 3 : 5 ]...) for k , v := range other { fmt . Printf ( "%d -> %s

" , k , v ) } // 0 -> a // 1 -> b // 2 -> d // 3 -> e

Maps data := map [ string ] string { "foo" : "bar" , "hello" : "world" , } for k , v := range data { fmt . Printf ( "%s -> %s

" , k , v ) } // hello -> world // foo -> bar

Packages package even func Even ( i int ) bool { return i % 2 == 0 } func odd ( i int ) bool { return i % 2 == 1 } // Even is exported Import ( "./even" "fmt" "os" ) os . Open ( filename ) fmt . Printf ( "Hello" ) even . Even ( 4 ) // Doesn't work: // even.odd(3)

Error handling f , err := os . Open ( name ) if err != nil { return err } if d , err := f . Stat (); err != nil { return err } doSomething ( f , d )

Defer func foobar ( name string ) error { f , err := os . Open ( name ) if err != nil { return err } defer f . Close () fmt . Printf ( "%v" , f ) return nil }