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
+11.9k Golang : List running EC2 instances and descriptions
+15.6k Golang : Read a file line by line
+21.8k Golang : Securing password with salt
+14.9k Golang : Get HTTP protocol version example
+8k Golang : Check if integer is power of four example
+11.4k Get form post value in Go
+6.9k Golang : Use modern ciphers only in secure connection
+5.9k Golang : Dealing with backquote
+6.6k Golang : Decode XML data from RSS feed
+5.8k nginx : force all pages to be SSL
+9.3k Golang : Validate IPv6 example
+8.5k Golang : Get final balance from bit coin address example