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.3k Golang : Intercept, inject and replay HTTP traffics from web server
+14.5k Golang : Overwrite previous output with count down timer
+15.2k Golang : Get timezone offset from date or timestamp
+9.3k Golang : Create unique title slugs example
+7.4k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+5.6k Unix/Linux : How to find out the hard disk size?
+6.6k Golang : Embedded or data bundling example
+7.6k Gogland : Where to put source code files in package directory for rookie
+22.9k Golang : Test file read write permission example
+6.3k Unix/Linux : Use netstat to find out IP addresses served by your website server
+10.1k Golang : How to tokenize source code with text/scanner package?
+5.1k Golang : Convert lines of string into list for delete and insert operation