Skip to content

Instantly share code, notes, and snippets.

@dvliman
Created July 26, 2013 02:47
Show Gist options
  • Save dvliman/6085711 to your computer and use it in GitHub Desktop.
Save dvliman/6085711 to your computer and use it in GitHub Desktop.
go
package main
import "fmt"
func main() {
// #=> slice is pass by reference
test := []int{1, 2}
fmt.Println(test)
modify(test)
fmt.Println(test)
fmt.Println()
fmt.Println()
// #=> append() returns the updated slice
// #=> therefore it is necessary to store the result of append?
test_append := []int{1, 2}
fmt.Println(test_append)
modify_append(test_append)
fmt.Println(test_append)
}
func modify(slice []int) {
slice[0] = 3
slice[1] = 4
return
}
func modify_append(slice[] int) {
slice = append(slice, 3)
return
}
// git:(master) ✗ go run test.go
// [1 2]
// [3 4]
// [1 2]
// [1 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment