Created
July 26, 2013 02:47
-
-
Save dvliman/6085711 to your computer and use it in GitHub Desktop.
go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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