2016-04-17 38 views
0

正如标题所示,我试图解析文件,但忽略注释(以#开头)或空白行。我试图为此制定一个系统,但它似乎总是忽视它应该忽略注释和/或空白行。解析文件,忽略注释和空行

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] 

我在这里的问题是,它依旧保持了“”和“#你好”的价值观,这意味着什么失败了,我一直无法弄清楚。

那么,我做错了什么,这保持不正确的价值观?

回答

2

len(line) > 0对于"# Hello"行将为真,因此它将被添加到output

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

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

如果len(行)> 0 && line [0]!='#'{...} – OneOfOne