I am trying to read data from a file in specific format.
file looks as follows
title:stack|content:overflow|metadata:53|comments:none
title:google|content:website|metadata:213|comments:Demos
I need to read this line by line and
assign title variable with title
value ("stack"
), content with content
value ("overflow"
) for each line.
scanner := bufio.NewScanner(file)
for scanner.Scan() {
data := scanner.Text()
data_arr := strings.Split(data, "|")
for _, n := range data_arr {
data_subdoc := strings.Split(n, ":")
a, b := data_subdoc[0], data_subdoc[1]
fmt.Println(a, b)
But problem is I get data as (relation between title, content, metadata and comments for each line is missing)
title stack
content overflow
metadata 53
comments none
title google
content website
metadata 213
comments Demos
But, I want something like:
stack overflow 53
if stack has 53:
print comments (in this case, its 'none')
google website 213
if google has 213, print content (In this case, its 'website')