In golang is there a nice way of getting a slice of values from a map?

前端 未结 5 1959
小蘑菇
小蘑菇 2020-12-23 15:38

If I have a map m is there a better way of getting a slice of the values v then

package main
import (
  \"fmt\"
)

func main() {
    m := make(map[int]string         


        
5条回答
  •  清歌不尽
    2020-12-23 16:35

    You can use this maps package:

    go get https://github.com/drgrib/maps
    

    Then all you have to call is

    values := maps.GetValuesIntString(m)
    

    It's type-safe for that common map combination. You can generate other type-safe functions for any other type of map using the mapper tool in the same package.

    Full disclosure: I am the creator of this package. I created it because I found myself rewriting these functions for map repeatedly.

提交回复
热议问题