Replace "0" with null, and it's no different from Java maps.
It seems like the strong/weak typing distinction: one value can be used freely like any other value, since that is what it is. The other can only be used as a regular value to a degree: it can be passed around, but not actually used.
public static void main(String[] args) {
Map<String, String> testHashMap = new HashMap<String, String>();
Map<String, String> testTreeMap = new TreeMap<String, String>();
testHashMap.put(null, "foobar");
testTreeMap.put(null, "foobar");
System.out.println(testHashMap.get(null));
System.out.println(testTreeMap.get(null));
}
This will throw NullPointerException at the last line :) For hashmaps it works, for treemaps not.Happened on production in my previous company (we used map2.get(map.get(...)) and used null rturned from the inner map to get the default value from the outer. Then we changed to tree maps to keep order when displaying the maps...
var m map[int]int = nil
a, ok := m[1] // returns 0, falseI prefer Python solution - throw Exception, and if programmer want default - there's always defaultdict.
map controlCenters = database.getControlCenters(); // returns map column->value, nil if no connection
int controlCentersInExistence = controlCenters["count"]; //FIXME - use the other syntax, I forgot how it looked like
if (controlCentersInExistence == 0) { // someone nuked USA!
WW3Manager.launchMissiles(); // we need to retaliate!
} controlCenters, err := database.getControlCenters()
if err != nil {
return fmt.Errorf("couldn't get map of control centers: %v", err)
}
// now we know controlCenters is valid.
controlCentersInExistence, ok := controlCenters["count"]
if !ok {
return errors.New("no count of control centers!")
}
// now we know the column existed
if controlCentersInExistence == 0 {
WW3Manager.launchMissiles()
}
Go's conventions are actually excellent at making this code MUCH more robust.BTW err != nil? I don't know, it seem fragile. Maybe it's years of exception indoctrination speaking, but I am scared that someone will return false for errors and someone else will check err != nil, or the reverse, or some other misunderstanding, and it will fail.
I'm confused. Previously you gave this example:
a, ok := m[1] // returns 0, false
Which means that `ok == false`. But how can it be false if that is not a valid value for an error?About your original point about someone returning "the wrong thing" in an error... there's really no misunderstanding possible. Because Go is statically typed, the ONLY things you can return for an error are nil (no error), or something that fulfills the error interface (a type with a method Error() that returns a string). You can't return false, or 0, or "" when the code expects you to return an error... the compiler won't let you.
This is how all error handling works in Go. From the most basic program you write, you learn that err == nil means success, anything else means failure.
Does that help any?
Wow, that's quite horrible (for a static language, anyway). So if my map returns `0`, I can't really be sure whether that is the zero-value (no value at this key), or that the value that I queried actually is `0`.
> This returns true or false in ok based on whether the value exists, zero value for foo if it doesn't exist.
Better, but still unnecessarily error-prone.
> however, go does have the compilation requirement that the ok value gets used somehow, or it'll give you an unused variable compilation error.
OK, I can see how that could alleviate the problem in practice: maybe in most contexts, there aren't anything more sensible to use the `ok` variable for than to check its value. In that case, the programmer will be alerted if he tries to use the `foo` variable without checking `ok` first.