Skip to content

Instantly share code, notes, and snippets.

@sahajre
Last active August 19, 2019 09:33
Show Gist options
  • Save sahajre/ec8ba50321fba2bc3cae841ce3751860 to your computer and use it in GitHub Desktop.
Save sahajre/ec8ba50321fba2bc3cae841ce3751860 to your computer and use it in GitHub Desktop.
package main
import "fmt"
func main() {
// Swapping with Assignment
i := 1
j := 2
i, j = j, i
fmt.Println(i, j) // 2 1
// with XOR
i = 1
j = 2
i = i ^ j
j = i ^ j
i = i ^ j
fmt.Println(i, j) // 2 1
// with multiplication and division
i = 1
j = 2
i = i * j
j = i / j
i = i / j
fmt.Println(i, j) // 2 1
// with addition and substraction
i = 1
j = 2
i = i + j
j = i - j
i = i - j
fmt.Println(i, j) // 2 1
// with temporary variable
i = 1
j = 2
temp := i
i = j
j = temp
fmt.Println(i, j) // 2 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment