Exercises
Interfaces and max()
In the maximum exercise we created a max function that works on a slice ofintegers. The question now is to create a program that shows the maximum numberand that works for both integers and floats. Try to make your program asgeneric as possible, although that is quite difficult in this case.
Answer
The following program calculates a maximum. It is as generic as you can get withGo.
package main
import "fmt"
func Less(l, r interface{}) bool { 1
switch l.(type) {
case int:
if _, ok := r.(int); ok {
return l.(int) < r.(int) 2
}
case float32:
if _, ok := r.(float32); ok {
return l.(float32) < r.(float32) 3
}
}
return false
}
func main() {
var a, b, c int = 5, 15, 0
var x, y, z float32 = 5.4, 29.3, 0.0
if c = a; Less(a, b) { 4
c = b
}
if z = x; Less(x, y) { 4
z = y
}
fmt.Println(c, z)
}
We could have chosen to make the return type of this 1 function aninterface{}
, but that would mean that a caller would always have to do a typeassertion to extract the actual type from the interface. At 2 we compare theparameters. All parameters are confirmed to be integers, so this is legit. Andat 3 we do the some for floats. At 4 we get the maximum value for a
, b
and x
and y
.
Pointers and reflection
One of the last paragraphs in section has the following words:
The code on the right works OK and sets the member
Name
to “Albert Einstein”. Of course this only works when you callSet()
with a pointer argument.
Why is this the case?
Answer
When called with a non-pointer argument the variable is a copy (call-by-value).So you are doing the reflection voodoo on a copy. And thus you are _not_changing the original value, but only this copy.