How to Swap Two Numbers in Golang?


In this tutorial, we will discuss swapping two numbers in Golang. We will cover two approaches: first swapping two numbers within the function and second creating a different function.

Swapping two numbers within the function

Algorithm

  • STEP 1 − Defining the variables that we want to Swap.

  • STEP 2 − Initializing the variables.

  • STEP 3 − Swapping the two variables within the function.

  • STEP 4 − Print the variables after swapping.

Example

package main // fmt package provides the function to print anything import "fmt" func main() { // define the variables we want to Swap var number1, number2, number3 int // initializing the variables number1 = 45 number2 = 63 // printing the numbers before swapping fmt.Println("Numbers before swapping: \n Number 1 =", number1,"\n Number 2 =", number2) // swapping the numbers number3 = number1 number1 = number2 number2 = number3 // printing the numbers after swapping fmt.Println("Numbers after swapping:\n Number 1 =", number1,"\n Number 2 =", number2, "\n(Swap within the function)") }

In the above code first, we are declaring two integer variables then we are initializing the variables. In the next step, we are swapping both the integer variables within the function using the third variable. In the end, we are printing the integer variable after swapping.

Output

Numbers before swapping:
   Number 1 = 45
   Number 2 = 63
Numbers after swapping:
   Number 1 = 63
   Number 2 = 45
(within the function)

Swapping two numbers using a different function

Algorithm

  • STEP 1 − Defining the variables that we want to add.

  • STEP 2 − Initializing the variables.

  • STEP 3 − Swapping the two variables using swapNumbers() function.

  • STEP 4 − Print the variables after swapping

Example

package main // fmt package provides the function to print anything import "fmt" func swapNumbers(number1, number2 *int) { var number3 int // swapping the numbers number3 = *number1 *number1 = *number2 *number2 = number3 } func main() { // define the variables we want to add var number1, number2 int // initializing the variables number1 = 45 number2 = 63 // printing the numbers before swapping fmt.Println("Numbers before swapping: \n Number 1 =", number1,"\n Number 2 =", number2) // swapping the numbers swapNumbers(&number1, &number2) // printing the numbers after swapping fmt.Println("Numbers after swapping:\n Number 1 =", number1,"\n Number 2 =", number2, "\n(Swap using different function)") }

In the above code first, we are declaring two integer variables then we are initializing the variables. In the next step, we are swapping both the integer variables using the swapNumber() function. In the end, we are printing the integer variable after swapping.

Syntax

number3 = *number1
*number1 = *number2
*number2 = number3

We are passing parameters as call by address so that value will get swapped globally due to which we are putting * in front of number 1 and number2. And in the above code, we are using a third variable to do the swapping. Storing number1 in number3 then number2 in number1 and then number3 in number1.

Output

Numbers before swapping:
   Number 1 = 45
   Number 2 = 63
Numbers after swapping:
   Number 1 = 63
   Number 2 = 45
(within the function)

Swapping two numbers with one line of code

Golang supports a syntax using which we can swap two variables in one line.

Syntax

Variable1, variable2 = variable2, variable1

Example

package main // fmt package provides the function to print anything import "fmt" func main() { // define the variables we want to add var number1, number2 int // initializing the variables number1 = 45 number2 = 63 // printing the numbers before swapping fmt.Println("Numbers before swapping: \n Number 1 =", number1,"\n Number 2 =", number2) // swapping the numbers in one line number1, number2 = number2, number1 // printing the numbers after swapping fmt.Println("Numbers after swapping:\n Number 1 =", number1,"\n Number 2 =", number2, "\n(Swap using a one-liner syntax)") }

In the above code first, we are declaring two integer variables then we are initializing the variables. In the next step, we are swapping both the integer variables using the one-line syntax supported by Golang. In the end, we are printing the integer variable after swapping

Syntax

number1, number2 = number2, number1 –

As Golang supports initializing multiple variables at the same time so here in this line we are initializing number1 with number2 and number2 with number1.

Output

Numbers before swapping:
   Number 1 = 45
   Number 2 = 63
Numbers after swapping:
   Number 1 = 63
   Number 2 = 45
(Swap using a one-liner syntax)

In these ways, we can swap two numbers in Golang. The separate function or one-liner lies under good programming standards. To learn more about Golang you can follow this tutorials.

Updated on: 26-Aug-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements