FAQ
Hi, I wrote a function that would return a sorted slice of strings from a
map[string]Foo. I'm curious what is the best way to create a generic
routine that can return a sorted slice of strings from any type that is a
map with strings as keys.

I know it can be done by passing interface{} and using the reflect module
to explicitly check the passed type. Is there a way to do it using an
interface specification? For example, is there any way to do something
like:

type MapWithStringKey interface {
..
}

To implement the interface above, a type would need strings as keys.

Thanks,
Bill

--

Search Discussions

  • Bryanturley at Nov 7, 2012 at 5:46 pm

    On Wednesday, November 7, 2012 11:04:10 AM UTC-6, Bill Katz wrote:
    Hi, I wrote a function that would return a sorted slice of strings from a
    map[string]Foo. I'm curious what is the best way to create a generic
    routine that can return a sorted slice of strings from any type that is a
    map with strings as keys.

    I know it can be done by passing interface{} and using the reflect module
    to explicitly check the passed type. Is there a way to do it using an
    interface specification? For example, is there any way to do something
    like:

    type MapWithStringKey interface {
    ..
    }

    To implement the interface above, a type would need strings as keys.

    Thanks,
    Bill
    map[string] interface {}
    ?

    You are going to have to do alot of type checking though.
    map[string] DataInterface could work as well as long as you could describe
    DataInterface to your liking.


    --
  • Bill Katz at Nov 7, 2012 at 8:32 pm
    You mean:

    type MapWithStringKey map[string]interface{}

    ??

    I would need an interface to avoid type coercion, right, so not what I'm
    looking for?

    -Bill
    On Wednesday, November 7, 2012 12:46:21 PM UTC-5, bryanturley wrote:
    On Wednesday, November 7, 2012 11:04:10 AM UTC-6, Bill Katz wrote:

    Hi, I wrote a function that would return a sorted slice of strings from a
    map[string]Foo. I'm curious what is the best way to create a generic
    routine that can return a sorted slice of strings from any type that is a
    map with strings as keys.

    I know it can be done by passing interface{} and using the reflect module
    to explicitly check the passed type. Is there a way to do it using an
    interface specification? For example, is there any way to do something
    like:

    type MapWithStringKey interface {
    ..
    }

    To implement the interface above, a type would need strings as keys.

    Thanks,
    Bill
    map[string] interface {}
    ?

    You are going to have to do alot of type checking though.
    map[string] DataInterface could work as well as long as you could
    describe DataInterface to your liking.
    --
  • Bryanturley at Nov 7, 2012 at 10:28 pm

    On Wednesday, November 7, 2012 2:32:29 PM UTC-6, Bill Katz wrote:
    You mean:
    type MapWithStringKey map[string]interface{}
    ??
    or just x := make(map[string] interface {})
    unless you want some methods on it.

    y, ok := map["yo"].(MagicStruct)
    if !ok {
    // not a MagicStruct
    }

    Wouldn't call it coercion, maybe inquisition?
    You can do a switch {} on types as well read the
    http://golang.org/ref/spec#Switch_statements under type switches not
    expression switches.


    --
  • Bill Katz at Nov 8, 2012 at 6:12 pm
    I've posted my best reflect-based solution under the StackOverflow question:

    http://stackoverflow.com/questions/13291958/does-go-allow-specification-of-an-interface-for-a-map-with-particular-key-type

    Looks like it's not possible to do it via an interface without generics.

    --
  • Bryanturley at Nov 8, 2012 at 6:51 pm
    You seem to be making a problem out of nothing...

    http://play.golang.org/p/BD4ane4qt6
    On Thursday, November 8, 2012 12:12:09 PM UTC-6, Bill Katz wrote:

    I've posted my best reflect-based solution under the StackOverflow
    question:


    http://stackoverflow.com/questions/13291958/does-go-allow-specification-of-an-interface-for-a-map-with-particular-key-type

    Looks like it's not possible to do it via an interface without generics.
    --
  • Bill Katz at Nov 8, 2012 at 8:05 pm
    This isn't about the # of lines of code which can be inlined for each
    type... it's about learning the limitations of interfaces as a substitute
    for duck typing in a language like python.
    On Thursday, November 8, 2012 1:51:29 PM UTC-5, bryanturley wrote:

    You seem to be making a problem out of nothing...

    http://play.golang.org/p/BD4ane4qt6
    On Thursday, November 8, 2012 12:12:09 PM UTC-6, Bill Katz wrote:

    I've posted my best reflect-based solution under the StackOverflow
    question:


    http://stackoverflow.com/questions/13291958/does-go-allow-specification-of-an-interface-for-a-map-with-particular-key-type

    Looks like it's not possible to do it via an interface without generics.
    --
  • Francesc Campoy Flores at Nov 8, 2012 at 8:14 pm
    I think I understand your question: how to get a list of keys from a
    map[string]Anything_here.

    In that case my code would be this: http://play.golang.org/p/N3s2GdvABE

    Note that it handles any case, without needing to add special cases as in
    the code you posted on StackOverflow. I hope this helps.

    On Thu, Nov 8, 2012 at 12:05 PM, Bill Katz wrote:

    This isn't about the # of lines of code which can be inlined for each
    type... it's about learning the limitations of interfaces as a substitute
    for duck typing in a language like python.

    On Thursday, November 8, 2012 1:51:29 PM UTC-5, bryanturley wrote:

    You seem to be making a problem out of nothing...

    http://play.golang.org/p/**BD4ane4qt6<http://play.golang.org/p/BD4ane4qt6>
    On Thursday, November 8, 2012 12:12:09 PM UTC-6, Bill Katz wrote:

    I've posted my best reflect-based solution under the StackOverflow
    question:

    http://stackoverflow.com/**questions/13291958/does-go-**
    allow-specification-of-an-**interface-for-a-map-with-**
    particular-key-type<http://stackoverflow.com/questions/13291958/does-go-allow-specification-of-an-interface-for-a-map-with-particular-key-type>

    Looks like it's not possible to do it via an interface without generics.
    --


    --
    --
    Francesc

    --
  • Francesc Campoy Flores at Nov 8, 2012 at 8:23 pm
    Or slightly shorter (and faster) http://play.golang.org/p/l_AimhZK8_


    On Thu, Nov 8, 2012 at 12:14 PM, Francesc Campoy Flores
    wrote:
    I think I understand your question: how to get a list of keys from a
    map[string]Anything_here.

    In that case my code would be this: http://play.golang.org/p/N3s2GdvABE

    Note that it handles any case, without needing to add special cases as in
    the code you posted on StackOverflow. I hope this helps.

    On Thu, Nov 8, 2012 at 12:05 PM, Bill Katz wrote:

    This isn't about the # of lines of code which can be inlined for each
    type... it's about learning the limitations of interfaces as a substitute
    for duck typing in a language like python.

    On Thursday, November 8, 2012 1:51:29 PM UTC-5, bryanturley wrote:

    You seem to be making a problem out of nothing...

    http://play.golang.org/p/**BD4ane4qt6<http://play.golang.org/p/BD4ane4qt6>
    On Thursday, November 8, 2012 12:12:09 PM UTC-6, Bill Katz wrote:

    I've posted my best reflect-based solution under the StackOverflow
    question:

    http://stackoverflow.com/**questions/13291958/does-go-**
    allow-specification-of-an-**interface-for-a-map-with-**
    particular-key-type<http://stackoverflow.com/questions/13291958/does-go-allow-specification-of-an-interface-for-a-map-with-particular-key-type>

    Looks like it's not possible to do it via an interface without generics.
    --


    --
    --
    Francesc

    --
    --
    Francesc

    --
  • Bill Katz at Nov 8, 2012 at 8:30 pm
    Thanks. Via MapKeys() it looks like you can get at map keys in a
    relatively type-independent way via reflect. Good to know.

    On Thursday, November 8, 2012 3:23:47 PM UTC-5, Francesc Campoy Flores
    wrote:
    Or slightly shorter (and faster) http://play.golang.org/p/l_AimhZK8_


    On Thu, Nov 8, 2012 at 12:14 PM, Francesc Campoy Flores <cam...@golang.org<javascript:>
    wrote:
    I think I understand your question: how to get a list of keys from a
    map[string]Anything_here.

    In that case my code would be this: http://play.golang.org/p/N3s2GdvABE

    Note that it handles any case, without needing to add special cases as in
    the code you posted on StackOverflow. I hope this helps.


    On Thu, Nov 8, 2012 at 12:05 PM, Bill Katz <bill...@gmail.com<javascript:>
    wrote:
    This isn't about the # of lines of code which can be inlined for each
    type... it's about learning the limitations of interfaces as a substitute
    for duck typing in a language like python.

    On Thursday, November 8, 2012 1:51:29 PM UTC-5, bryanturley wrote:

    You seem to be making a problem out of nothing...

    http://play.golang.org/p/**BD4ane4qt6<http://play.golang.org/p/BD4ane4qt6>
    On Thursday, November 8, 2012 12:12:09 PM UTC-6, Bill Katz wrote:

    I've posted my best reflect-based solution under the StackOverflow
    question:

    http://stackoverflow.com/**questions/13291958/does-go-**
    allow-specification-of-an-**interface-for-a-map-with-**
    particular-key-type<http://stackoverflow.com/questions/13291958/does-go-allow-specification-of-an-interface-for-a-map-with-particular-key-type>

    Looks like it's not possible to do it via an interface without
    generics.
    --


    --
    --
    Francesc

    --
    --
    Francesc
    --
  • Bryanturley at Nov 8, 2012 at 8:26 pm

    On Thursday, November 8, 2012 2:05:32 PM UTC-6, Bill Katz wrote:
    This isn't about the # of lines of code which can be inlined for each
    type... it's about learning the limitations of interfaces as a substitute
    for duck typing in a language like python.
    If you are only comparing go's interfaces to similar concepts in other
    langauges, you are missing the point of go's interfaces.
    They are unique to go You can simulate some other langauge's features with
    them but that is not what they are.

    --
  • Stevewang at Nov 8, 2012 at 7:24 pm

    On Thursday, November 8, 2012 1:04:10 AM UTC+8, Bill Katz wrote:
    Hi, I wrote a function that would return a sorted slice of strings from a
    map[string]Foo. I'm curious what is the best way to create a generic
    routine that can return a sorted slice of strings from any type that is a
    map with strings as keys.

    I know it can be done by passing interface{} and using the reflect module
    to explicitly check the passed type. Is there a way to do it using an
    interface specification? For example, is there any way to do something
    like:

    type MapWithStringKey interface {
    ..
    }

    To implement the interface above, a type would need strings as keys.
    Here you are:

    1. type MapWithStringKey interface {
    2. Keys() []string
    3. }
    4.
    5. func TheFunctionYouWant(itf Interface) []string {
    6. keys := itf.Keys()
    7. sort.Strings(keys)
    8. return keys
    9. }


    Thanks,
    Bill
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 7, '12 at 5:04p
activeNov 8, '12 at 8:30p
posts12
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase