Q&A
Ask and answer questions to make information more available to wider audiences.
Layla Johns @johnslayla   05, Jun 2023 12:00 AM
swap variables
How can we swap variables in Golang?
answers 1
 
Answer 1
Lily Jarvi @jarvilily   12, Jun 2023 05:18 PM
func swap(sw []int) {

        for a, b := 0, len(sw)-1; a < b; a, b = a+1, b-1 {

                sw[a], sw[b] = sw[b], sw[a]

        }



}
func main() {

x := []int{3, 2, 1}

swap(x)

fmt.Println(x)

// Output: [1 2 3]

}