Go 正在分析文件,忽略注释和空行

Go 正在分析文件,忽略注释和空行,go,Go,正如标题所说,我试图解析一个文件,但忽略了注释(以开头)或空行。我已经试着为这做了一个系统,但它似乎总是忽略了它应该忽略注释和/或空行 lines := strings.Split(d, "\n") var output map[string]bool = make(map[string]bool) for _, line := range lines { if strings.HasPrefix(line, "#") != true { output[line] =

正如标题所说,我试图解析一个文件,但忽略了注释(以
开头)或空行。我已经试着为这做了一个系统,但它似乎总是忽略了它应该忽略注释和/或空行

lines := strings.Split(d, "\n")
var output map[string]bool = make(map[string]bool)

for _, line := range lines {
    if strings.HasPrefix(line, "#") != true {
        output[line] = true
    } else if len(line) > 0 {
        output[line] = true
    }
}
运行时(这是函数的一部分),它输出以下内容

This is the input ('d' variable):
Minecraft
Zerg Rush
Pokemon

# Hello

This is the output when printed ('output' variable):

map[Minecraft:true Zerg Rush:true Pokemon:true :true # Hello:true]
我这里的问题是它仍然保留了“”和“#Hello”值,这意味着有些东西失败了,有些东西我还没有弄清楚

那么,这会保留不正确的值,这有什么错呢?

len(line)>0
对于
“#Hello”
行将为真,因此它将被添加到
输出中

目前,您正在添加不是以#开头或不是空的行。只需添加满足以下两个条件的行:

if !strings.HasPrefix(line, "#") && len(line) > 0 {
    output[line] = true
}
len(line)>0
对于
“#Hello”
行将为真,因此它将被添加到
输出中

目前,您正在添加不是以#开头或不是空的行。只需添加满足以下两个条件的行:

if !strings.HasPrefix(line, "#") && len(line) > 0 {
    output[line] = true
}

如果len(line)>0&&line[0]!='{…}
如果len(line)>0&&line[0]!='{…}