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

[Q] How to print the contents of a Set? #110

Closed wsw70 closed 1 year ago

wsw70 commented 1 year ago

I built a Set of string and I now would like to print it out

fmt.Printf("%v", mySet) outputs Set{a, b, ...} but I do not know how to actually get the bare strings inside (to be able, for instance, to strings.Join() them)

deckarep commented 1 year ago

Hello,

You have a few options: you can either call the ToSlice and convert the set to a slice where you can reference the strings directly.

Or a better way is to just iterate the Set directly using the Each function which takes a callback. This is ideal because you can just leave the set as-is and do updates and then iterate when needed.