On Thu, Oct 18, 2018 at 10:37 PM Rusco <j.reb...@gmail.com> wrote: > > I just tried this Google Playground recipe from Bradfritz: > > https://play.golang.org/p/RYac90kI-H > > (seen via https://github.com/golang/go/issues/21095) > > and noted that y appears in the output but x appears as <nil>. > > Can someone explain me why ? I was expecting that x appears in the same way > like y.
It is because of the NaN in the struct. NaN==x is always false for all x, so NaN containing instances of S are not equal even though all the non-NaN members are equal. Duplicate the line that adds "x", and set it to "z", and you'll have three elements in the map, two of which with identical keys and nil value. > > > package main > > import ( > "fmt" > "math" > ) > > var a, b int > > func main() { > type S struct { > x int16 > y float64 > z interface{} > p *int > } > m := map[S]string{} > m[S{1, math.NaN(), "foo", &a}] = "x" > m[S{2, 3.0, &a, &b}] = "y" > for i := 0; i < 20; i++ { > fmt.Printf("%+v\n", m) > } > } > > //output: > > map[{x:2 y:3 z:0x1b5400 p:0x1b5404}:y {x:1 y:NaN z:foo p:0x1b5400}:<nil>] > map[{x:1 y:NaN z:foo p:0x1b5400}:<nil> {x:2 y:3 z:0x1b5400 p:0x1b5404}:y] > > > > -- > 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.