避免在 for 循环中"declared but not used"错误的技巧?



我是Go新手。而这个";声明但未使用";错误真的很重要。我的意思是,令人讨厌。

这是我的代码:

func addBorder(a []string) []string {
var result []string
wall := ""
for i := range a {
newElement := "*" + a[i] + "*"
fmt.Println(newElement)
result = append(result, newElement)
}
for i := range a[0] {
wall += "*"
}
result = append([]string{wall}, result...)
return result
}

问题是第二个for循环。它一直在说";我声明但没有使用";一直都不知道如何处理它。除了用它迭代之外,我真的不需要它。添加i += 0解决了这个问题,但这绝对不是解决这个问题的好方法。有人能启发我吗?谢谢

在second-for-loop中,您可以这样编码:

for range a[0] {
wall += "*"
}

相关内容

最新更新