
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Reverse a Slice
In this tutorial, we will learn how to reverse a slice using variety of examples. A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas slice is a dynamic array which means its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover they are passed by reference instead by value. Let's go through the example to understand things.
Syntax
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.
Method 1: Using integer values in the example
In this method, we will see how to reverse a slice using integer values. Here we will intake the values using append function. The output will be printed on the console using fmt.Println() function. Let's see through the code how is all being done.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 ? Create a function main and in that function create an empty slice of type integer and add elements into that slice using append function.
Step 3 ? Run a loop with two variables i and j both applied in same place.
Step 4 ? Initialize the variables i and j equals to 0 and length of slice -1. Run the loop till I is less than j.
Step 5 ? In the loop using Go's multiple assignment feature swap the elements at indices i and j.
Step 6 ? After swapping i is incremented by 1 and j is decremented by 1 and the loop continues for the next swap.
Step 7 ? Print the swapped slice using fmt.Prinltn() function where ln refers to the new line.
Example
Golang program to reverse a slice using integer values in the example
package main import "fmt" func main() { var slice []int slice = append(slice, 10) // create slice using append function slice = append(slice, 20) slice = append(slice, 30) slice = append(slice, 40) fmt.Println("The slice before reversal is:", slice) for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 { slice[i], slice[j] = slice[j], slice[i] //reverse the slice } fmt.Println("The slice after reversal is:") fmt.Println(slice) // print the reversed slice }
Output
The slice before reversal is: [10 20 30 40] The slice after reversal is: [40 30 20 10]
Method 2: Using string values in the example
In this method, we will see how to reverse a slice using string values. Here we will intake the values using append function. The output will be printed on the console using fmt.Println() function. Let's see through the code how is all being done.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 ? Create a function main and in that function create an empty slice of type string and add elements into that slice using append function.
Step 3 ? Run a loop with two variables i and j both applied in same place.
Step 4 ? Initialize the variables i and j equals to 0 and length of slice -1. Run the loop till I is less than j.
Step 5 ? In the loop using Go's multiple assignment feature swap the elements at indices i and j.
Step 6 ? After swapping i is incremented by 1 and j is decremented by 1 and the loop continues for the next swap.
Step 7 ? Print the swapped slice using fmt.Prinltn() function where ln refers to the new line.
Example
Glang program to reverse a slice using string values in the example
package main import "fmt" func main() { var slice []string slice = append(slice, "Delhi") // create slice using append function slice = append(slice, "Mumbai") slice = append(slice, "Bangalore") slice = append(slice, "Jaipur") fmt.Println("The slice before reversal is:", slice) for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 { slice[i], slice[j] = slice[j], slice[i] // reverse the slice } fmt.Println("The slice after reversal is:") fmt.Println(slice) // print the reversed slice }
Output
The slice before reversal is: [Delhi Mumbai Bangalore Jaipur] The slice after reversal is: [Jaipur Bangalore Mumbai Delhi]
Conclusion
We executed the program of reversing the elements of a slice using two examples. In the first example we used integer values and swapped them and in the second example we used the string values and swapped them. Both the examples give similar output. Hence the program executed successfully.