package main
import "fmt"
func main() {
a := 1
b := 2
fmt.Printf("Before Swap: %v %v\n", a, b)
a, b = b, a
fmt.Printf(" After Swap: %v %v\n", a, b)
}
The output is:
Before Swap: 1 2
After Swap: 2 1
It looks good. I just wonder:
Is it safe to swap two integers by a, b = b, a in golang?
Yes, it is. When you have same variable type, you can swipe them.
From the docs
In assignments, each value must be assignable to the type of the operand to which it is assigned, with the following special cases: