package main
import (
"fmt"
"big"
)
func main() {
var x map[*big.Rat]string = make(map[*big.Rat]string)
var onehalf = big.NewRat(1,2)
x[onehalf] = "this is 1/2"
var otherhalf = big.NewRat(1,2)
val, ok := x[onehalf]
fmt.Printf("val = %v, ok = %v\n", val, ok)
val, ok = x[otherhalf]
fmt.Printf("val = %v, ok = %v\n", val, ok)
}
The example code prints:
val = this is 1/2, ok = true
val = , ok = false
I am expecting:
val = this is 1/2, ok = true
val = this is 1/2, ok = true
Is there some way to overcome this problem? If not, are there any
plans for fixing this problem? (perhaps by using a Comparator
interface implemented by big.Rat).
I am building an interpreter using go. The language is great! But
after two months of development using go I found out a map
limitation.
I can not find a way to have custom comparison/hashing for map keys.
In the following example I am expecting the rational number 1/2 to be
the same key for the map even if it is stored in a different address.
You are using confusing pointers and values. For example,
package main
import (
"fmt"
)
func main() {
var one = new(int)
var otherone = new(int)
fmt.Println(*one, *otherone, *one == *otherone)
fmt.Println(one, otherone, one == otherone)
}
Output:
0 0 true
0xf840029018 0xf840029020 false
Peter
I am building an interpreter using go. The language is great! But
after two months of development using go I found out a map
limitation.
I can not find a way to have custom comparison/hashing for map keys.
In the following example I am expecting the rational number 1/2 to be
the same key for the map even if it is stored in a different address.