×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

How would you succinctly swap the values of two variables in Go language?

Here, we will learn the easiest (succinctly) way to swap the values of two variables in Golang.
Submitted by IncludeHelp, on October 03, 2021

The values can be swapped by using the following way,

x, y = y, x

Example:

package main

import (
	"fmt"
)

func main() {
	x := 10
	y := 20

	fmt.Printf("Before swapping, x: %d, y: %d\n", x, y)
	x, y = y, x
	fmt.Printf("After swapping, x: %d, y: %d\n", x, y)
}

Output:

Before swapping, x: 10, y: 20
After swapping, x: 20, y: 10

Golang FAQ »



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.