TruffleGo Compiler

Hello World

First, here's the classic "Hello World" program.

package main import "fmt"

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

To run the program, put the code into your favorite text editor and save the file as hello-world.go. Then, use the command ./gt hello-world.go. If you would like to see the Go output alongside the Truffle output, use the flag -go. It will print out any difference.

$ ./gt hello-world.go Hello World!

$ ./gt hello-world.go TruffleOutput: Hello World! Go Output: Hello World! Any diff listed below:

Values

Go has several types including strings, integers, floats, and booleans.

package main import "fmt"

func main() { fmt.Println("Hello" + " World!") fmt.Println("4 + 2 =", 4 + 2) fmt.Println("1.0/3.0 =", 1.0/3.0) fmt.Println(true) }

$ ./gt values.go Hello World! 4 + 2 = 6 1.0/3.0 = 0.33333334 true

Variables

In Go, there are several ways to declare variables.

package main import "fmt"

func main() { var x = "Hello" fmt.Println(x)
var a, b int = 4, 2 fmt.Println(a, b)
var y = true fmt.Println(y)
var z int fmt.Println(z)
g := 7 fmt.Println(g) }

$ ./gt variables.go Hello 4 2 true 0 7

For

Go's only looping construct is for. There are three basic types: single conditions, a for without a condition exited only by a break or a return, and a classic initial/condition/after for loop.

package main import "fmt"

func main() { i := 1 for i < 4 { fmt.Println(i) i += 1 }
for { fmt.Println("break") break }
for j := 4; j < 7; j++ { fmt.Println(j) } }

$ ./gt forLoop.go 1 2 3 break 4 5 6

Switch

Switch statements are a way of writing conditionals.

package main import "fmt"

func main() { i := 1 switch i { case 1: fmt.Println("one") case 2: fmt.Println("two") case 3: fmt.Println("three") default: fmt.Println("Not one, two, or three") } }

$ ./gt switch.go one

If/Else

If statements in Go are the another way to write conditionals.

package main import "fmt"

func main() { if 8%2 == 0 { fmt.Println("Eight is even") } else { fmt.Println("Eight is odd") }
if 10 % 3 != 0 { fmt.Println("Ten is not divisible by three") }
if x := 3; x < 0 { fmt.Println("x is negative") } else if x < 2 { fmt.Println("x is less than two") } else { fmt.Println("x is greater than two") } }

$ ./gt if.go Eight is even Ten is not divisible by three x is greater than two

Arrays

An array is a sequence of elements of predetermined length.

package main import "fmt"

func main() { var arr [7]int fmt.Println(arr) arr[0] = 10 arr[4] = 70 fmt.Println("Length: ", len(arr)) x := [10]int{10,9,8,7,6,5,4,3,2,1} fmt.Println(x) }

$ ./gt array.go [0 0 0 0 0 0 0] Length: 7 [10 9 8 7 6 5 4 3 2 1]

Slices

Slices are like arrays, except slices support several more operators. A slice can have a value appended to it, and support the "slice" operator with the syntax slice[low:high].

package main import "fmt"

func main() { s := make([]string, 3) s[0] = "a" s[1] = "b" s[2] = "c" s = append(s, "d") fmt.Println(s)
x := s[1:3] fmt.Println(x) }

$ ./gt slice.go [a b c d] [b c]

Maps

Maps store key/value pairs.

package main import "fmt"

func main() { m := make(map[string]int) m["k1"] = 7 m["k2"] = 13 fmt.Println("map:", m) delete(m,"k1") fmt.Println(m)
a := map[string]int{"foo": 1, "bar": 2} a["foo"] = 10 a["bar"] = 3 fmt.Println(a) }

$ ./gt map.go map: map[k1:7 k2:13] map[k2:13] map[foo:3 bar:2 stuff:10]

Functions

Functions in Go are fairly simple. You explicitly declare types for each variable.

package main import "fmt"

func sum(x int, y int) int { return x + y }

func threeSum(x int, y int, z int) int { return x + y + z }

func main() { a := sum(2,4) b := threeSum(2,4, a) fmt.Println(a) fmt.Println(b) }

$ ./gt functions.go 6 12

Multiple Returns

Functions in Go can have multiple returns.

package main import "fmt"

func vals() (int, int) { return 4, 2 }

func main() { a, b := vals() fmt.Println(a, b) }

$ ./gt multipleReturns.go 4 2

Recursion

Go supports recursive functions.

package main import "fmt"

func fib(x int) int { if x == 0 { return 0 }
if x == 1 { return 1 } return fib(x-1) + fib(x-2) }

func main() { fmt.Println(fib(7)) }

$ ./gt recursion.go 13

Structs

Go supports structs.

package main import "fmt"

type Vertex struct { X int Y int }

func main() { v := Vertex{X:1, Y:2} fmt.Println(v.X) }

$ ./gt struct.go 1

Struct Methods

Struct methods are defined on struct types.

package main import "fmt"

type Rectangle struct { width int height int }

func (r Rectangle) area() int { return r.width * r.height }

func main() { r := Rectangle{width:4, height:6} fmt.Println(r.area()) }

$ ./gt structMethods.go 24