mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-21 05:40:59 +00:00
Translated function: learnErrorHandling()
This commit is contained in:
parent
5a174230a3
commit
2b1e58f22c
@ -226,19 +226,21 @@ func learnInterfaces() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func learnErrorHandling() {
|
func learnErrorHandling() {
|
||||||
// ", ok" idiom used to tell if something worked or not.
|
// Das ", ok" Idiom wird häufig verwendet um zu überprüfen ob etwas schief
|
||||||
m := map[int]string{3: "three", 4: "four"}
|
// gegangen ist.
|
||||||
if x, ok := m[1]; !ok { // ok will be false because 1 is not in the map.
|
m := map[int]string{3: "drei", 4: "vier"}
|
||||||
fmt.Println("no one there")
|
if x, ok := m[1]; !ok { // ok wird false sein, da 1 nicht in der map ist.
|
||||||
|
fmt.Println("keine eins gefunden")
|
||||||
} else {
|
} else {
|
||||||
fmt.Print(x) // x would be the value, if it were in the map.
|
fmt.Print(x) // x wäre der Wert, wenn er in der map wäre.
|
||||||
}
|
}
|
||||||
// An error value communicates not just "ok" but more about the problem.
|
// Ein Fehler-Wert (error value) gibt mehr Informationen über den Grund für
|
||||||
if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value
|
// das Problem an.
|
||||||
// prints "strconv.ParseInt: parsing "non-int": invalid syntax"
|
if _, err := strconv.Atoi("nicht-int"); err != nil { // _ verwirft den Wert
|
||||||
|
// Gibt: "strconv.ParseInt: parsing "nicht-int": invalid syntax" aus
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
// We'll revisit interfaces a little later. Meanwhile,
|
// Wir kommen bald nochmal auf Interfaces zurück. Aber inzwischen:
|
||||||
learnConcurrency()
|
learnConcurrency()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user