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
+19.5k Golang : Archive directory with tar and gzip
+5.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+14.8k Golang : Save(pipe) HTTP response into a file
+21.9k Golang : Repeat a character by multiple of x factor
+8.4k Golang : Convert(cast) []byte to io.Reader type
+13.4k Generate salted password with OpenSSL example
+12.8k Golang : Convert(cast) uintptr to string example
+16.6k Golang : Set up source IP address before making HTTP request
+5.8k Linux/MacOSX : Search for files by filename and extension with find command
+34.3k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+6.1k Golang : Test input string for unicode example
+6.9k Golang : Use modern ciphers only in secure connection