GO语言:读取文件并将内容转化为数组

我想在main上面的函数中实现这个数组,但是如何实现?


hosts := []string{"inanzzz1@100.79.154.22", "inanzzz2@200.79.190.11"}


JSON 文件内容:


inanzzz@inanzzz-VirtualBox:~/go$ go run reader.go < hosts.txt 

{

   {

      "username":"inanzzz1",

      "ip":"100.79.154.22"

   },

   {

      "username":"inanzzz2",

      "ip":"200.79.190.11"

   }

}

读取上面的 JSON 文件的 GO 文件:


package main


import (

    "os"

    "bufio"

    "fmt"

)


func main() {

    r := bufio.NewReader(os.Stdin)

    line, err := r.ReadString('\n')

    for i := 1; err == nil; i++ {

        //fmt.Printf("Line %d: %s", i, line)

        fmt.Printf(line)

        line, err = r.ReadString('\n')

    }

}


梦里花落0921
浏览 853回答 3
3回答

ITMISS

假设您拥有的是一个 json 数组(第一个和最后一个 {} 由 [] 替换)而不是 hosts.txt 中描述的无效 JSON,那么您就有了一个有效的解决方案:package mainimport (&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os")type UsernameIp struct {&nbsp; &nbsp; Username string `json:"username"`&nbsp; &nbsp; Ip&nbsp; &nbsp; &nbsp; &nbsp;string `json:"ip"`}func main() {&nbsp; &nbsp; j := json.NewDecoder(os.Stdin)&nbsp; &nbsp; var src []UsernameIp&nbsp; &nbsp; j.Decode(&src)&nbsp; &nbsp; var hosts []string&nbsp; &nbsp; for _, h := range src {&nbsp; &nbsp; &nbsp; &nbsp; entry := fmt.Sprintf("%s@%s", h.Username, h.Ip)&nbsp; &nbsp; &nbsp; &nbsp; hosts = append(hosts, entry)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(hosts)}正确的 hosts.txt 文件:[&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; "username":"inanzzz1",&nbsp; &nbsp; &nbsp; "ip":"100.79.154.22"&nbsp; &nbsp;},&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; "username":"inanzzz2",&nbsp; &nbsp; &nbsp; "ip":"200.79.190.11"&nbsp; &nbsp;}]

慕码人8056858

您可能应该先解组 json,然后迭代结果。首先 - 使 hosts.txt 成为有效的 JSON。为此使用[]而不是{}:[&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; "username":"inanzzz1",&nbsp; &nbsp; &nbsp; "ip":"100.79.154.22"&nbsp; &nbsp;},&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; "username":"inanzzz2",&nbsp; &nbsp; &nbsp; "ip":"200.79.190.11"&nbsp; &nbsp;}]然后解组。这是完整的示例:包主导入(“fmt”“编码/json”)功能主(){&nbsp; type host struct {&nbsp; &nbsp;Username string&nbsp;&nbsp; &nbsp;Ip string&nbsp;&nbsp;}cont := `&nbsp; &nbsp; [&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; "username":"inanzzz1",&nbsp; &nbsp; &nbsp; "ip":"100.79.154.22"&nbsp; &nbsp;},&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; "username":"inanzzz2",&nbsp; &nbsp; &nbsp; "ip":"200.79.190.11"&nbsp; &nbsp;}]&nbsp;`// Read file to some byte array. We will use string for this example.var arr []hosterr := json.Unmarshal([]byte(cont), &arr)&nbsp;if err != nil {&nbsp; &nbsp;&nbsp; &nbsp; fmt.Println(err)}&nbsp;hosts := make([]string, len(arr))for i, h := range arr {&nbsp; &nbsp;hosts[i] = h.Username + "@" + h.Ip}&nbsp;fmt.Println(hosts)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

Cats萌萌

您可以使用ioutil.ReadFile将整个文件作为一个字符串读取,该字符串是一个 json 文档,然后将该字符串解组为一个结构,以便于访问:type user_ip struct{&nbsp; &nbsp; username string `json:"username"`&nbsp; &nbsp; ip string&nbsp; &nbsp; &nbsp; &nbsp;`json:"ip"`}type jsonStruct struct{&nbsp; &nbsp; sample []user_ip}func main() {&nbsp; &nbsp; //you'll have to read file here&nbsp; &nbsp; str := `{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "username":"inanzzz1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ip":"100.79.154.22"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "username":"inanzzz2",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ip":"200.79.190.11"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }`&nbsp; &nbsp; userIP := jsonStruct{}&nbsp; &nbsp; err := json.Unmarshal(str, &userIP)&nbsp; &nbsp; panic(err)&nbsp; &nbsp; //do what you want with the struct here}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go