0

I know this works.

for i :=range []int{1, 2, 3....} {
    fmt.Println(i)
}

But If I want to do something like:

for i :=range []int{1, 2, 3....} {
    code = GenNewCode()
    Insert(code)
}

I get an error that i was not used. Is there a way I can do it without getting the above error? (Pardon me if this is a silly question, I am just learning Golang a bit.)

1
  • 2
    No, that's valid: play.golang.org/p/WFHSrsjISC Please show an example with the error your'e seeing Commented Dec 21, 2016 at 19:17

1 Answer 1

1

You can ignore such things by using the blank identifier: _

for _ := range []int{1, 2, 3} {
    code = GenNewCode()
    Insert(code)
}

Or one can use (via JimB's comment)

for range []int{1, 2, 3}{
code = GenNewCode()
    Insert(code)
}
Sign up to request clarification or add additional context in comments.

3 Comments

You don't need the _, just use for range []int{1, 2, 3}
Awesome! Learnt one more thing. Thanks!
Also when using blank identifier, the : will cause an error no new variables on left side of := ; it must be for _ = range []int{1, 2, 3} ; JimB's answer is cleaner though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.