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
+5.9k Golang : Measure execution time for a function
+5.6k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+6.8k Golang : Takes a plural word and makes it singular
+7.1k Golang : Of hash table and hash map
+8.8k Golang : Inject/embed Javascript before sending out to browser example
+7.9k Golang : Tell color name with OpenCV example
+32k Golang : Math pow(the power of x^y) example
+4.9k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+7.7k Golang : Grayscale Image
+29.2k Golang : Login(Authenticate) with Facebook example
+7.2k Golang : How to convert strange string to JSON with json.MarshalIndent
+21.5k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload