file, err := os.Open("file1")
// complains if you don't check err
file2, err := os.Open("file2")
// no problem, no need to check err.if _, err := os.Open("file.go"); err != nil {
//handle error
}
// Do stuff with file
but you need to split that into 2 lines, just like the good article says i.e. file, err := os.Open("abcd")
if err != nil {
// <handle os.Open(...) errors
}
otherwise, outside the 'if {...}' block, 'file' is undefined.
As a sidenote, it would be more idiomatic to do this:
This has bitten me before - if you use a non-existent key on a map of strings (or dictionary, or hash, however you call it) you get "" and an error back. value is "", ok is 'false'.What type you get back exactly from a bad request depends on the type of call, but you always get an error back! Maps storing ints return 0, maps storing strings return "", etc.
Go always forces you to look at each error, since errors are always different.