deckarep / golang-set

A simple, battle-tested and generic set type for the Go language. Trusted by Docker, 1Password, Ethereum and Hashicorp.
Other
4.15k stars 274 forks source link

ToSlice() from string slice returns numbers #65

Closed maginemu closed 5 years ago

maginemu commented 5 years ago

Simple example

s := mapset.NewSetFromSlice([]interface{}{"foo", "bar", "baz"})

fmt.Println(s)

for item := range s.ToSlice() {
    fmt.Println(item)
}

produces below:

Set{foo, bar, baz}
0
1
2

Is this designed behavior?

I expected below:

Set{foo, bar, baz}
foo
bar
baz

(I found I could use s.Iter() to produce expected output though)

deckarep commented 5 years ago

Hello,

You are seeing numbers because your for loop is incorrect. In Go, for loops that use the range keyword supply an index value only in the default case. If you want the actual value you need to write it like the following:

for _, item := range s.ToSlice(){
  fmt.Println(item)
}
maginemu commented 5 years ago

Hello, thank you for your kind reply. I got it.