deckarep / golang-set

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

How I can get every elements in my set? #119

Closed GHY188 closed 1 year ago

GHY188 commented 1 year ago

mpst := mapset.NewSet("1", "2", "3", "4") fmt.Println(mpst.Cardinality()) for _, elem := range mpst{ fmt.Println(elem) } This is my code, and goland shows me: can not range over 'mpst', how I can get every elements in my set?

deckarep commented 1 year ago

Hello,

The range keyword only works with Go’s built-in types and Go doesn’t really have a genuine iterator pattern that works generically with range loops.

So, this API allows you to iterate the contents using the Each API which takes a callback.

See the following test case for an example: https://github.com/deckarep/golang-set/blob/054f2368d789505d67f3dbe630e5e4af44d299bc/set_test.go#L955

GHY188 commented 1 year ago

thx for your reply! I really appreciate it! ---- Replied Message ---- | From | Ralph @.> | | Date | 05/26/2023 01:57 | | To | deckarep/golang-set @.> | | Cc | ghy @.>, Author @.> | | Subject | Re: [deckarep/golang-set] How I can get every elements in my set? (Issue #119) |

Hello,

The range keyword only works with Go’s built-in types and Go doesn’t really have a genuine iterator pattern that works generically with range loops.

So, this API allows you to iterate the contents using the Each API which takes a callback.

See the following test case for an example: https://github.com/deckarep/golang-set/blob/054f2368d789505d67f3dbe630e5e4af44d299bc/set_test.go#L955

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>