2015-07-23 20:34
浏览 577


I have the following in my json array (conf.json file).

  "Repos": [

I am attempting to read this json and then iterate over it but get stuck. I am very new to go (and to programming) so I am having a hard time understanding what is happening here.

import (

type Configuration struct {
    Repos []string

func read_config() {
    file, _ := os.Open("conf.json")
    decoder := json.NewDecoder(file)
    configuration := Configuration{}
    err := decoder.Decode(&configuration)
    if err != nil {
        fmt.Println("error:", err)


So far this is as far as I have been able to get. This will print out the values okay, [a, b, c].

What I would like to do is be able to iterate over the array and split out each value individually but have not had any luck at doing this. Am I taking the wrong approach to this? Is there a better way to do this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsux90368 2015-07-23 20:38

    You mean something like this:

    for _, repo := range configuration.Repos {

    Note that the code in your example should not work with the JSON that you have given. There is no mapping between value and Repos. You either have posted incorrect JSON or omitted a tag on the Configuration struct to map it correctly.

    点赞 打赏 评论
  • dte29947 2015-07-23 20:55

    Everything worked fine, it's just your printing that is not doing what you expect. Since Repos is an array you'll have to iterate it to print each value individually. Try something like this;

    for _, repo := range configuration.Repos {
    点赞 打赏 评论

相关推荐 更多相似问题