-1

I am new to Go. And this "declared but not used" error is really something. I mean, annoying.

Here is my code:

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
}

The problem is the second for loop. It keeps saying that "i declared but not used" all the time and no idea how to deal with it. I don't really need it besides using it to iterate. Adding i += 0 solved it but that is definitely not a good way to deal with this problem. Can anyone enlighten me? Thanks.

Auclown
  • 179
  • 1
  • 3
  • 20
  • @AbdulNiyasPM I have looked at that post and tried it, but it ended up with another error. Don't remember what it was but doesn't seem that appears anymore. Thanks. – Auclown Aug 27 '20 at 03:14

1 Answers1

3

In second for loop, you can code like that:

for range a[0] {
    wall += "*"
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
hczhang.cn
  • 46
  • 4