美文网首页
【Golang】 读取文件并解析成map

【Golang】 读取文件并解析成map

作者: 冉小妹Ran | 来源:发表于2019-03-07 15:31 被阅读0次

文件保存内容(whitelist.csv):

{
        "a":      true,
        "b":      true,
        "c":      true,
        "d":      true,
        "e":      true
    }

处理代码(main.go):

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
)

func main() {
    b, err := ioutil.ReadFile("C:\\Users\\admin2\\Desktop\\whitelist.csv")
    if err != nil {
        fmt.Print(err)
    }

    whitelist := map[string]bool{}
    err = json.Unmarshal([]byte(string(b)), &whitelist)
    if err != nil {
        panic(err)
    }

    for key, value := range whitelist {
        fmt.Println("key", key, "value", value)
        fmt.Printf("%T", value)
    }
}

相关文章

网友评论

      本文标题:【Golang】 读取文件并解析成map

      本文链接:https://www.haomeiwen.com/subject/adgcpqtx.html