doutuoben6908 2016-05-22 20:42
浏览 18
已采纳

实例化结构和数组不会创建新的引用

I'm attempting to convert a simple markdown file into json, the markdown looks something like this:

#TITLE 1
- Line 1
- Line 2
- Line 3

#TITLE 2
- Line 1
- Line 2
- Line 3
<!-- blank line -->

I can't understand what's required to refactor the following in func main():

    type Section struct {
        Category string
        Lines    []string
    }

    file, _ := os.Open("./src/basicmarkdown/basicmarkdown.md")

    defer file.Close()

    rgxRoot, _ := regexp.Compile("^#[^#]")
    rgxBehaviour, _ := regexp.Compile("^-[ ]?.*")

    scanner := bufio.NewScanner(file)

    ruleArr := []*Section{}
    rule := &Section{}

    for scanner.Scan() {

        linetext := scanner.Text()

        // If it's a blank line
        if rgxRoot.MatchString(linetext) {
            rule := &Section{}
            rule.Category = linetext
        }

        if rgxBehaviour.MatchString(linetext) {
            rule.Lines = append(rule.Lines, linetext)
        }

        if len(strings.TrimSpace(linetext)) == 0 {
            ruleArr = append(ruleArr, rule)
        }

    }

    jsonSection, _ := json.MarshalIndent(ruleArr, "", "\t")
    fmt.Println(string(jsonSection))

The code above outputs:

[
{
        "Category": "",
        "Lines": [
            "- Line 1",
            "- Line 2",
            "- Line 3",
            "- Line 1",
            "- Line 2",
            "- Line 3"
        ]
    },
    {
        "Category": "",
        "Lines": [
            "- Line 1",
            "- Line 2",
            "- Line 3",
            "- Line 1",
            "- Line 2",
            "- Line 3"
        ]
    }
]

When I was hoping to output:

[
    {
        "Category": "#TITLE 1",
        "Lines": [
            "- Line 1",
            "- Line 2",
            "- Line 3"
        ]
    },
    {
        "Category": "#TITLE 2",
        "Lines": [,
            "- Line 1",
            "- Line 2",
            "- Line 3"
        ]
    }
]

Couple of things wrong for sure. Please excuse the verbosity of the question, hard to explain without an example when you're a noob. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongtui6347 2016-05-22 21:04
    关注

    Inside the for loop, take a closer look to this part:

    // If it's a blank line
    if rgxRoot.MatchString(linetext) {
        rule := &Section{} // Notice the `:=`
        rule.Category = linetext
    }
    

    You're basically creating a new rule variable in the scope of that if, when you probably want to reuse the one you have already created outside the for loop.

    So, try changing it to:

    // If it's a blank line
    if rgxRoot.MatchString(linetext) {
        rule = &Section{} // Notice the `=`
        rule.Category = linetext
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序