D
D
Dmitry Shnyrev2014-11-04 20:58:19
go
Dmitry Shnyrev, 2014-11-04 20:58:19

Set of unique values ​​(Set) in Go?

I wondered about getting a list of parent Ids from subordinate records.
But so that it was a list of unique values.
Usually in other programming languages ​​I use Set for this, but I did not find anything similar in Go.
But I found this solution:

uIds := make(map[string]bool)
for _, a := range articles {
  uIds[strconv.Itoa(a.UserId)] = true
}
uIdsSet := make([]string, 0, len(uIds))
for k := range uIds {
  uIdsSet = append(uIdsSet, k)
}

Is this a normal solution? Or is there a more elegant way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Lerg, 2014-11-04
@dmnBrest

I am using AppendIfMissing
stackoverflow.com/a/9561388
But your solution is not bad either.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question