On Fri, Jan 25, 2019 at 7:47 AM Victor Giordano <vitucho3...@gmail.com> wrote: > > Just wanna say : It would be nice to employ a custom type as a valid key for > a map!!! :D
You can use a struct as a key. > > > El jueves, 24 de enero de 2019, 0:47:32 (UTC-3), mount...@gmail.com escribió: >> >> thanks lan. >> >> 在 2019年1月23日星期三 UTC+8下午11:26:57,Ian Lance Taylor写道: >>> >>> On Tue, Jan 22, 2019 at 11:47 PM <mount...@gmail.com> wrote: >>> > >>> > i got : >>> > # command-line-arguments >>> > ./key.go:6:18: invalid operation: F() == F() (func can only be compared >>> > to nil) >>> >>> Yes, that is what the language spec says and it is what I said: in Go, >>> function values are not comparable. >>> >>> I wrote that code as an example to help explain why function values >>> are not comparable. >>> >>> Ian >>> >>> > 在 2019年1月23日星期三 UTC+8上午11:59:07,mount...@gmail.com写道: >>> >> >>> >> When i use the func, map slice as the key of map, it isn't work! >>> >> So I lookup source why, i find >>> >> >>> >> // spec: "The comparison operators == and != must be fully defined >>> >> // for operands of the key type; thus the key type must not be a >>> >> // function, map, or slice." >>> >> // >>> >> // Delay this check because it requires fully setup types; >>> >> // it is safe to continue in any case (was issue 6667). >>> >> check.later(func() { >>> >> if !Comparable(typ.key) { >>> >> check.errorf(e.Key.Pos(), "invalid map key type %s", >>> >> typ.key) >>> >> } >>> >> }) >>> >> // Comparable reports whether values of type T are comparable. >>> >> func Comparable(T Type) bool { >>> >> switch t := T.Underlying().(type) { >>> >> case *Basic: >>> >> // assume invalid types to be comparable >>> >> // to avoid follow-up errors >>> >> return t.kind != UntypedNil >>> >> case *Pointer, *Interface, *Chan: >>> >> return true >>> >> case *Struct: >>> >> for _, f := range t.fields { >>> >> if !Comparable(f.typ) { >>> >> return false >>> >> } >>> >> } >>> >> return true >>> >> case *Array: >>> >> return Comparable(t.elem) >>> >> } >>> >> return false >>> >> } >>> > >>> > -- >>> > You received this message because you are subscribed to the Google Groups >>> > "golang-nuts" group. >>> > To unsubscribe from this group and stop receiving emails from it, send an >>> > email to golang-nuts...@googlegroups.com. >>> > For more options, visit https://groups.google.com/d/optout. > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.