Golang : Convert lines of string into list for delete and insert operation
Problem:
You want to convert a list of items in text format into a list. With the list, you want to search for a certain element by traversing the nodes. Once you found the element, you want to delete the element and insert a new element. i.e replace the element.
In another word, you have a text file that you want to transform into a list of nodes such as a double linked list where you can push
, pop
and traverse the tree with front
and back
functions. How to do that?
Solution:
Below is a program that will transform text string into a list with the help of container/list
package. It also demonstrates how easy it is to remove a string that fit certain search value and insert a new element with the value of "Hello World". It also demonstrates how to convert the list back to a text string.
Here you go!
package main
import (
"container/list"
"fmt"
"strings"
)
func main() {
textString := `line 1
line 2
line 3
line 4`
lines := strings.Split(textString, "\n")
// NOTE : To read from text file
// see https://www.socketloop.com/tutorials/golang-display-a-text-file-line-by-line-with-line-number-example
listOfLines := list.New()
// push each line into listOfLines
for _, line := range lines {
listOfLines.PushBack(line)
}
// before pop
for e := listOfLines.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value.(string))
}
// search for line 3 and pop(remove) the element
for e := listOfLines.Front(); e != nil; e = e.Next() {
if e.Value.(string) == "line 3" {
listOfLines.Remove(e)
}
}
fmt.Println("=============================================")
// after pop
for e := listOfLines.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value.(string))
}
// now, let's insert Hello World after line 2
for e := listOfLines.Front(); e != nil; e = e.Next() {
if e.Value.(string) == "line 2" {
listOfLines.InsertAfter("Hello World!", e)
}
}
fmt.Println("=============================================")
backToStringSlice := make([]string, listOfLines.Len())
// after inserting Hello World and convert back to string
// don't forget to add "\n" at the end of each line
i := 0
for e := listOfLines.Front(); e != nil; e = e.Next() {
backToStringSlice[i] = e.Value.(string) + "\n"
i++
}
// convert back from slice to string
toString := fmt.Sprintf("%v", backToStringSlice)
// remove the prefix [ and suffix ] leftover
//fmt.Println(string(toString[0]))
//fmt.Println(string(toString[len(toString)-1]))
toString = toString[1 : len(toString)-1]
// and we're back to text
fmt.Println(toString)
}
Output:
line 1
line 2
line 3
line 4
=============================================
line 1
line 2
line 4
=============================================
line 1
line 2
Hello World!
line 4
References:
See also : Golang : Turn string or text file into slice example
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
+39.7k Golang : UDP client server read write example
+6.1k Unix/Linux : Use netstat to find out IP addresses served by your website server
+5.3k Python : Print unicode escape characters and string
+39.2k Golang : Remove dashes(or any character) from string
+27k Golang : Convert CSV data to JSON format and save to file
+8.9k Golang : Gonum standard normal random numbers example
+7.1k Golang : Accessing dataframe-go element by row, column and name example
+7.4k Golang : Command line ticker to show work in progress
+8k Swift : Convert (cast) Character to Integer?
+10.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+5.5k List of Golang XML tutorials