f, err := os.Open("filename.txt")
if err == nil {
fmt.Println("f")
} else {
fmt.Println(err)
//SAME AS
fmt.Println(err.Error())
}
import (
"errors"
)
myError := errors.New("My error string")
Panic, Defer, and Recover
Puts a func on the call stack that will be run in reverse order when host ft finishes
resp, _ := http.Get("http://golang.org")
defer func () {
if resp != nil {
resp.Body.Close()
}
}()
body, _ := ioutil.ReadAll(resp.Body)
func panic(v interface{})
func Perform() (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
GoesWrong()
return
}
func GoesWrong() {
panic(errors.New("Fail"))
}
func main() {
err := Perform()
fmt.Println(err)
}
attendance := map[string]bool {
"ann": true,
}
attended, ok := attendance["M"]
if ok {
fmt.Println("M?", attended)
} else {
fmt.Println("No info")
}