Swap Two Values

Swap two variables using a temporary variable.

package main

import "fmt"

func main() {
	a := "left"
	b := "right"
	fmt.Println("Before:", a, b)

	temp := a
	a = b
	b = temp
	fmt.Println("After:", a, b)
}
▶ Open Go Playground

Copy the code above and paste to run

© 2026 ByteLearn.dev. Free courses for developers. · Privacy