How to differentiate two custom struct types in Golang with same data as keys in map? - dictionary

type Student struct {
id int,
name string,
}
//If I create two structs,
s := Student{1, "jack"}
t := Student{1, "jack"}
m := make(map[Student]bool)
m[s] = true
m[t] = true
1) Will this work?
2) Is there anything similar to Java's hashCode in Golang?

If two struct values have the same member values, and if all those members are comparable, then those structs are equal. In your example: s==t is true. Struct value itself is the map key. So in your example, there will be only one element in the map at the end.
More on comparability:
https://golang.org/ref/spec#Comparison_operators
There is no equivalent of hashCode in Go.

1) Will this work?
Yes, of course. But you map will contain just one entry.
2) Is there anything similar to Java's hashCode in Go[...]?
No.

Related

How to make composite key for a hash map in go

First, my definition of composite key - two ore more values combine to make the key. Not to confuse with composite keys in databases.
My goal is to save computed values of pow(x, y) in a hash table, where x and y are integers. This is where I need ideas on how to make a key, so that given x and y, I can look it up in the hash table, to find pow(x,y).
For example:
pow(2, 3) => {key(2,3):8}
What I want to figure out is how to get the map key for the pair (2,3), i.e. the best way to generate a key which is a combination of multiple values, and use it in hash table.
The easiest and most flexible way is to use a struct as the key type, including all the data you want to be part of the key, so in your case:
type Key struct {
X, Y int
}
And that's all. Using it:
m := map[Key]int{}
m[Key{2, 2}] = 4
m[Key{2, 3}] = 8
fmt.Println("2^2 = ", m[Key{2, 2}])
fmt.Println("2^3 = ", m[Key{2, 3}])
Output (try it on the Go Playground):
2^2 = 4
2^3 = 8
Spec: Map types: You may use any types as the key where the comparison operators == and != are fully defined, and the above Key struct type fulfills this.
Spec: Comparison operators: Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.
One important thing: you should not use a pointer as the key type (e.g. *Key), because comparing pointers only compares the memory address, and not the pointed values.
Also note that you could also use arrays (not slices) as key type, but arrays are not as flexible as structs. You can read more about this here: Why have arrays in Go?
This is how it would look like with arrays:
type Key [2]int
m := map[Key]int{}
m[Key{2, 2}] = 4
m[Key{2, 3}] = 8
fmt.Println("2^2 = ", m[Key{2, 2}])
fmt.Println("2^3 = ", m[Key{2, 3}])
Output is the same. Try it on the Go Playground.
Go can't make a hash of a slice of ints.
Therefore the way I would approach this is mapping a struct to a number.
Here is an example of how that could be done:
package main
import (
"fmt"
)
type Nums struct {
num1 int
num2 int
}
func main() {
powers := make(map[Nums]int)
numbers := Nums{num1: 2, num2: 4}
powers[numbers] = 6
fmt.Printf("%v", powers[input])
}
I hope that helps
Your specific problem is nicely solved by the other answers. I want to add an additional trick that may be useful in some corner cases.
Given that map keys must be comparable, you can also use interfaces. Interfaces are comparable if their dynamic values are comparable.
This allows you to essentially partition the map, i.e. to use multiple types of keys within the same data structure. For example if you want to store in your map n-tuples (it wouldn't work with arrays, because the array length is part of the type).
The idea is to define an interface with a dummy method (but it can surely be not dummy at all), and use that as map key:
type CompKey interface {
isCompositeKey() bool
}
var m map[CompKey]string
At this point you can have arbitrary types implementing the interface, either explicitly or by just embedding it.
In this example, the idea is to make the interface method unexported so that other structs may just embed the interface without having to provide an actual implementation — the method can't be called from outside its package. It will just signal that the struct is usable as a composite map key.
type AbsoluteCoords struct {
CompKey
x, y int
}
type RelativeCoords struct {
CompKey
x, y int
}
func foo() {
p := AbsoluteCoords{x: 1, y: 2}
r := RelativeCoords{x: 10, y: 20}
m[p] = "foo"
m[r] = "bar"
fmt.Println(m[AbsoluteCoords{x: 10, y: 20}]) // "" (empty, types don't match)
fmt.Println(m[RelativeCoords{x: 10, y: 20}]) // "bar" (matches, key present)
}
Of course nothing stops you from declaring actual methods on the interface, that may be useful when ranging over the map keys.
The disadvantage of this interface key is that it is now your responsibility to make sure the implementing types are actually comparable. E.g. this map key will panic:
type BadKey struct {
CompKey
nonComparableSliceField []int
}
b := BadKey{nil, []int{1,2}}
m[b] = "bad!" // panic: runtime error: hash of unhashable type main.BadKey
All in all, this might be an interesting approach when you need to keep two sets of K/V pairs in the same map, e.g. to keep some sanity in function signatures or to avoid defining structs with N very similar map fields.
Playground https://play.golang.org/p/0t7fcvSWdy7

How does a pointer to a struct or array value in Go work?

Considering the following Go struct:
type Person struct {
Name string
Age int
Country string
}
I have encountered numerious times the following use:
p := &Person{"Adam", 33, "Argentina"}
Yet I can not see the point in pointing to a struct value, and I wonder, how does it differ from:
n := &999 // Error
My questions are:
How is it even possible to point to a value, even if it is a struct or array and not a primitive like a string or int? Strange enough, the following doesn't contribute to my understanding:
fmt.Println(p, &p) // outputs: &{Adam 33 Argentina} 0xc042084018
Why would a programmer want to declare a struct instance by a pointer? What could you achieve doing so?
&Person{} is a language "construct", it's part of the spec: it allocates a new variable of Person type, and provides you the address of that anonymous variable.
Spec: Composite literals:
Taking the address of a composite literal generates a pointer to a unique variable initialized with the literal's value.
Also: Spec: Variables:
Calling the built-in function new or taking the address of a composite literal allocates storage for a variable at run time.
&999 is not allowed by the language spec. The possible operands of the address operators are listed in the Spec: Address operators:
The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal.
p := Person{} creates a new variable p whose type will be Person. p := &Person{} creates a new variable whose type will be *Person.
See possible duplicate: How do I do a literal *int64 in Go?
When you print the values with the fmt package, it has certain rules how to print values of different types:
For compound objects, the elements are printed using these rules, recursively, laid out like this:
struct: {field0 field1 ...}
array, slice: [elem0 elem1 ...]
maps: map[key1:value1 key2:value2]
pointer to above: &{}, &[], &map[]
When you use fmt.Println(), the default formatting rules will be applied, which for a value of type *int is the %p verb, which will print the memory address in hexadecimal format, but for a pointer to struct it prints the struct value prepended with an & sign (&{}). You can read more about it in related question: Difference between golang pointers
If you want to print the pointed value, dereference the pointer and pass the pointed value, e.g.:
var p = new(int)
*p = 12
fmt.Println(*p) // Prints 12
As to why to create a pointer to a value (and not a value), see these related questions:
Pointers vs. values in parameters and return values
Why should constructor of Go return address?
Go, X does not implement Y (... method has a pointer receiver)

How can I prevent a type being used as a map key?

I have a type that can be used as a map key, but I want to prevent this from occurring. I assumed that if the type contained a private member it wouldn't be possible from other packages, but this appears to work anyway. What's the best way to make the type unusable as a map key?
type MyType struct {
A *A
b b
preventUseAsKey ?
}
I don't see any benefit of disallowing a type being used as a key. It is just an option which may or may not be used, the type will not be any better or smaller or faster just because you forbid to use it as a map key.
But if you want to do it: Spec: Map types:
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.
So if you can violate the terms of the comparison operators, you implicitly get what you want. You have a struct, terms for the struct types:
Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.
So struct values are only comparable (and thus can only be used as keys in maps) if all their fields are comparable. Simply add a field whose type is not comparable.
Slice, map, and function values are not comparable.
So for example add a field whose type is a slice, and you're done:
type MyType struct {
S string
i int
notComparable []int
}
Attempting to use the above MyType as a key:
m := map[MyType]int{}
You get a compile-time error:
invalid map key type MyType
Note:
I wrote about not having any benefit of forbidding the type being a key. It's more than that: from now on you won't be able to use comparison operators on values of your type anymore (because of the extra, non-comparable field), so e.g. you lose the option to compare those values:
p1, p2 := MyType{}, MyType{}
fmt.Println(p1 == p2)
Compile-time error:
invalid operation: p1 == p2 (struct containing []int cannot be compared)
Note that with a little trick you could still preserve the comparable nature of your type, e.g. by not exporting your type but a wrapper type which embeds the original one; and add the extra, non-comparable type to the wrapper type, e.g.:
type myType struct {
S string
i int
}
type MyType struct {
myType
notComparable []int
}
func main() {
p1, p2 := MyType{}, MyType{}
fmt.Println(p1.myType == p2.myType)
}
This way your myType can remain comparable but still prevent the exported, wrapper MyType type to be used as key type.
Your type should not be comparable in order to be unfit as a map key.
Slice, map, and function values are not comparable
See Key Type:
Notably absent from the list are slices, maps, and functions; these types cannot be compared using ==, and may not be used as map keys.
So if your type is a slice, map or function, you should get what you need.
It could be an "alias" (defining a new named type):
type StringSliceWrap []string
type MyFunc func(i int)
That alias would not be used as a map key.
Update 2017: Brad Fitzpatrick give this tip (adding a slice in your struct) to make sure your type struct is not comparable: See play.golang.org:
package main
// disallowEqual is an uncomparable type.
// If you place it first in your struct, you prevent == from
// working on your struct without growing its size. (Don't put it
// at the end; that grows the size of the struct)
type disallowEqual [0]func()
type T struct {
_ disallowEqual
Foo string
Bar int
}
func main() {
var t1 T
var t2 T
println(t1 == t2)
}
T cannot be used as amp key now!

Does Go (deep) copy keys when inserting into a map?

I have a map with complex keys - for example, 2D arrays:
m := make(map[[2][3]int]int)
When I insert a new key into the map, does Go make a deep copy of the key?
a := [2][3]int{{1, 2, 3}, {4, 5, 6}}
m[a] = 1
In other words, if I change the array a after using it as a map key, does the map still contain the old value of a?
Short answer, it is copied.
By specification, Arrays are value types.
Go's arrays are values. An array variable denotes the entire array; it is not a pointer to the first array element (as would be the case in C). This means that when you assign or pass around an array value you will make a copy of its contents. (To avoid the copy you could pass a pointer to the array, but then that's a pointer to an array, not an array.)
https://blog.golang.org/go-slices-usage-and-internals
See for yourself:
https://play.golang.org/p/fEUYWwN-pm
package main
import (
"fmt"
)
func main() {
m := make(map[[2][3]int]int)
a := [2][3]int{{1, 2, 3}, {4, 5, 6}}
fmt.Printf("Pointer to a: %p\n", &a)
m[a] = 1
for k, _ := range m {
fmt.Printf("Pointer to k: %p\n", &k)
}
}
The pointers do not match.
EDIT: The real reason is when inserting into a map, the key value is copied. Or, you can continue to just remember the rule above: arrays are value types and their reuse denotes a copy. Either works here. :)
Arrays are always passed by value, so, yes in this case Go will make a deep copy of the key.
From the language 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. If the key type is an interface type, these comparison operators must be defined for the dynamic key values; failure will cause a run-time panic.
The keys are copied into the map. Excluding map and slice as valid keys means that the keys can't change. Note that go doesn't follow pointers if you define a map type with a pointer as a key (eg map[*int]int) it compares the pointers directly.

In Go, what's the inverse of reflect.SliceOf()?

In Go, reflect.SliceOf() makes a Type representing a slice of the given Type:
SliceOf returns the slice type with element type t. For example, if t represents int, SliceOf(t) represents []int.
However, I already have a Type for []int but want to get a Type for int. Is there an easy way to do that? (Note, I'm using int as an example. In reality, all I know is I have a slice, and I need to find what Type each element of the slice is.)
I'm trying to populate a slice of bool, int, float, or string, from a []string using reflection... here's the relevant piece:
numElems := len(req.Form["keyName"])
if structField.Kind() == reflect.Slice && numElems > 0 {
slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
for i := 0; i < numElems; i++ {
// I have some other code here to fill out the slice
}
}
But in order to fill out the slice, I need to know the type of the slice I'm filling out...
In your case, you already have the element type: structField.Type(). You can use reflect.New(t).Elem() to get an "editable" reflect.Value of a type. Once you've populated that value, you can call slice.Index(i).Set(...) to assign that value into the resulting slice.
To answer the letter of your question though, if you do have a slice and need to populate it, say you have a reflect.Type of a []int, then you can call .Elem() to get the reflect.Type for int.
See the Type documentation for the methods you can call on a Type.

Resources