Skip to content

Instantly share code, notes, and snippets.

@bootandy
Created May 17, 2013 16:18
Show Gist options
  • Save bootandy/5600200 to your computer and use it in GitHub Desktop.
Save bootandy/5600200 to your computer and use it in GitHub Desktop.
go notes
Note return type on end of function:
func add(x int, y int) int {
return x + y
}
Can be shortened to:
func add(x, y int) int {
return x + y
}
Can return tuples:
func swap(x, y string) (string, string) {
return y, x
}
Works out which vars to return by naming:
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
Constants:
const Pi = 3.14
For loop. No () allowed. {} required (horay). [also with if]
sum := 1
for i := 0; i < 10; i++ {
sum += i
}
For is also While
sum := 1
for sum < 1000 {
sum += sum
}
fmt.Println(sum)
Variable scope bound by {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment