Linked Questions

Popular Questions

how to assign values from a file line in to a map in Golang

Asked by At

I am trying to write a self of key value pairs, later be used as a rule engine. Here what i am trying to achieve is getting the following code to make a map called "f" and assign the values on the file line to it. However the following code throws an exception saying "f is not a type"

Is the method im doing to achieve the above task is correct? if not please suggest a better way of doing it, and better if you can provide me with a sample code How to assign the line value to the map? better if you can provide me with a sample code

Thank you in advance

rules file

"name": "hero", "age":"27
"name": "villein", "age":"30

code

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
)

func main() {
    file, err := os.Open("rules")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        var f = make(map[string]string)
        f {scanner.Text()}
        fmt.Println(f)
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}

Related Questions