3 ms·
First of all, that wouldn't work in go, it would complain that you don't use the err variable, you could change it to: file, _ := os.Open("file.go") The u
by a_bonobo 12y ago
First of all, that wouldn't work in go, it would complain that you don't use the err variable, you could change it to:
file, _ := os.Open("file.go")
The underscore tells the compiler to ignore the error. And yes, file is <nil> now!
As a sidenote, it would be more idiomatic to do this:
if _, err := os.Open("file.go"); err != nil {
//handle error
}
// Do stuff with file
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.
my_hash := make(map[string]string)
value, ok := my_hash["no_key"]
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.
- jamwt 12y agofile, err := os.Open("file1") // complains if you don't check err file2, err := os.Open("file2") // no problem, no need to check err.
- signa11 12y agoif _, 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.