Golang : Reverse text lines or flip line order example
Problem:
You are given lines of text and you need to reverse the lines order before encrypting the text. Reversing or scrambling the lines of text order can be useful in situation when you want extra layer of protection against decryption program. For example, from
abc
def
to
def
abc
How to do that?
Solution:
package main
import (
"fmt"
"strings"
)
func main() {
// originalOrder := `abc
//def
//ghi
//jkl
//mno
//pqr
//stu
//vwx
//yz`
originalOrder := `Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Praesent porttitor nulla vitae interdum fermentum. Ut in vulputate neque.
Praesent luctus lacus a dolor tempus pellentesque. Cras sit amet urna eu augue suscipit eleifend.
Mauris mollis pharetra faucibus. Phasellus eu massa quam.
Nunc id metus placerat neque feugiat commodo.`
lines := strings.Split(originalOrder, "\n")
fmt.Println("Before :\n ", originalOrder)
reverseOrderSlice := []string{}
// https://www.socketloop.com/tutorials/golang-how-to-reverse-slice-or-array-elements-order
//reverse the elements in lines slice
for i := range lines {
n := strings.TrimSpace(lines[len(lines)-1-i])
reverseOrderSlice = append(reverseOrderSlice, n+"\n")
}
fmt.Println("=============================================")
// but we want the end result to be lines
toLines := fmt.Sprintf("%v", reverseOrderSlice)
// remove the prefix [ and suffix ] leftover
toLines = toLines[1 : len(toLines)-1]
fmt.Println("After :\n ", toLines)
}
Output:
Before :
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Praesent porttitor nulla vitae interdum fermentum. Ut in vulputate neque.
Praesent luctus lacus a dolor tempus pellentesque. Cras sit amet urna eu augue suscipit eleifend.
Mauris mollis pharetra faucibus. Phasellus eu massa quam.
Nunc id metus placerat neque feugiat commodo.
=============================================
After :
Nunc id metus placerat neque feugiat commodo.
Mauris mollis pharetra faucibus. Phasellus eu massa quam.
Praesent luctus lacus a dolor tempus pellentesque. Cras sit amet urna eu augue suscipit eleifend.
Praesent porttitor nulla vitae interdum fermentum. Ut in vulputate neque.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
References:
https://www.socketloop.com/tutorials/golang-how-to-reverse-slice-or-array-elements-order
See also : Golang : How to reverse slice or array elements order
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+3.5k Java : Random alphabets, alpha-numeric or numbers only string generator
+12.3k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+7.5k Golang : Reverse a string with unicode
+9.2k Golang : Convert(cast) string to int64
+7.1k Android Studio : How to detect camera, activate and capture example
+5.8k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+12.4k Golang : Remove or trim extra comma from CSV
+10.2k Swift : Convert (cast) String to Integer
+8.4k Golang : Set or add headers for many or different handlers
+33.7k Golang : Proper way to set function argument default value
+15.3k Golang : How to convert(cast) IP address to string?
+8.6k Golang : Get final balance from bit coin address example