GO Program - Swap Two Numbers

Поділитися
Вставка
  • Опубліковано 27 чер 2024
  • How to Swap Two Numbers in Go language.
    There are two ways in which you can swap two numbers in Go language.
    The first one is to use multiple assignment.
    And the second is to use a temporary variable.
    Using multiple assignment
    =======================
    Let us take two values in a and b.
    Now, write the multiple assignment. We assign a, b with b, a. When we do this in a single step, the values in an and b shall be swapped.
    Using temporary variable
    =======================
    First we initialize temporary variable with value in b. This is like a backup for the value in b.
    Assign the value in variable a to variable b. Now, the variable b holds the value of a, and a also holds the value of a.
    We have a copy of b in temporary variable. Use that value to copy the original value of b to a.
    This is how we Swap Two Numbers in Go language.

КОМЕНТАРІ •