Golang Program to reverse the elements of the array using inbuilt function


In this tutorial, we will write a go language program to reverse the elements of the array using inbuilt functions. in this program we will see how we can reverse an array of strings and integers using internal go functions.

Method 1: Using Append() and Make() Function

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Algorithm

STEP 1 − First, we need to import the fmt package.

STEP 2 − Now, start the main() function. Inside this function use the make() function to create an array of integers and enter values to it using append() function.

STEP 3 − Print the array on the screen using fmt.Println() function. Now create a new empty array of integers to store the reversed array.

STEP 4 − Use for loop to iterate over every element of the array and use append function to store the the elements from the array to empty array in reverse order.

STEP 5 − Once, the whole array is iterated and the elements are stored at their respective places, print the final array on the screen.

Example 1

In this example, we will write a golang program to reverse the elements of the array of integers using append() function.

package main
import "fmt"
func main() { 

   // initializing an array
   array := make([]int, 0, 5)
   array = append(array, 1, 2, 3, 4, 5)
   fmt.Println("The given array is:", array)
   var rev []int
   for _, n := range array {
      rev = append([]int{n}, rev...)
   }
   fmt.Println("The new array formed after reversing the above array is:", rev)
}

Output

The given array is: [1 2 3 4 5]
The new array formed after reversing the above array is: [5 4 3 2 1]

Example 2

In this example, we will write a golang program to reverse the elements of the array of strings using append() function.

package main
import "fmt"
func main() {

   // initializing an array
   array := make([]string, 0, 5)
   array = append(array, "x", "y", "z", "a", "b")
   fmt.Println("The given array is:", array)
   var rev []string
   for _, n := range array {
      rev = append([]string{n}, rev...)
   }
   fmt.Println("The new array formed after reversing the above array is:", rev)
}

Output

The given array is: [x y z a b]
The new array formed after reversing the above array is: [b a z y x]

Method 2: Using Appned() and Slice() Functions

In this method, we will write a golang program to reverse the elements of the array of integers using Slice() function present in sort package.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

func Slice(x any, less func(i, j int) bool)

The Slice() function is present in sort package and is used to sort a slice of array in any manner. This function takes two arguments one is the array which is to be sorted and the other argument is a function which states the order in which the given slice to be sorted. The function then returns a Boolean variable as result.

Algorithm

STEP 1 − First, we need to import the fmt package.

STEP 2 − Now, start the main() function. Inside this function use the make() function to create an array of integers and enter values to it using append() function.

STEP 3 − Print the array on the screen using fmt.Println() function. Now create a new empty array of integers to store the reversed array.

STEP 4 − Use for loop to iterate over every element of the array and use append function to store the the elements from the array to empty array in reverse order.

STEP 5 − Once, the whole array is iterated and the elements are stored at their respective places, print the final array on the screen.

Example

Golang program to reverse the elements of the array using slice function

package main
import (
   "fmt"
   "sort"
)
func main() {

   // initializing an array
   array := make([]int, 0, 5)
   array = append(array, 12, 21, 34, 45, 50)
   fmt.Println("The given array is:", array)
   sort.Slice(array, func(i, j int) bool {
      return true
   })
   fmt.Println("The new array formed after reversing the above array is:", array)
}

Output

The given array is: [12 21 34 45 50]
The new array formed after reversing the above array is: [50 45 34 21 12]

Conclusion

We have successfully compiled and executed a go language program to reverse all elements of the array along with examples. in the first example we have used various internal functions to achieve this result.

Updated on: 09-Feb-2023

769 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements