Created
March 28, 2015 10:36
-
-
Save thekarel/25f23f03b7ba9ff97a25 to your computer and use it in GitHub Desktop.
Swap 2 variables with Go pointers.
http://play.golang.org/p/troDfxY2zy
http://www.golang-book.com/8/index.htm
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 swap(x, y *int) { | |
var z = *x | |
*x = *y | |
*y = z | |
} | |
func main() { | |
x := 3 | |
y := 2 | |
fmt.Println(x, y) | |
swap(&x, &y) | |
fmt.Println(x, y) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lines 6-8 can also be written as *x, *y = *y, *x