1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-10 00:36:48 +02:00

Translated function: learnControlFlow()

This commit is contained in:
Joseph Adams
2014-06-01 16:41:17 +02:00
parent 23b6e66f8b
commit 67e28c96b9

View File

@@ -137,54 +137,57 @@ func expensiveComputation() int {
} }
func learnFlowControl() { func learnFlowControl() {
// If statements require brace brackets, and do not require parens. // Bedingte Anweisungen verlangen nach geschweiften Klammern, normale
// Klammern um die Bedingung werden aber nicht gebraucht.
if true { if true {
fmt.Println("told ya") fmt.Println("hab's dir ja gesagt!")
} }
// Formatting is standardized by the command line command "go fmt." // Die Formattierung ist durch den Befehl "go fmt" standardisiert
if false { if false {
// pout // nicht hier
} else { } else {
// gloat // sonder hier! spielt die Musik
} }
// Use switch in preference to chained if statements.
// Benutzen Sie ein "switch" Statement anstatt eine Anreihung von if-s
x := 1 x := 1
switch x { switch x {
case 0: case 0:
case 1: case 1:
// cases don't "fall through" // Einzelne Fälle fallen nicht zum nächsten durch!
case 2: case 2:
// unreached // wird nicht ausgeführt
} }
// Like if, for doesn't use parens either. // Wie bei "if", braucht "for" auch keine Klammern um die Bedingung
for x := 0; x < 3; x++ { // ++ is a statement for x := 0; x < 3; x++ { // ++ ist ein Statement
fmt.Println("iteration", x) fmt.Println(x, "-te Iteration")
} }
// x == 1 here. // Ab hier gilt wieder: x == 1
// For is the only loop statement in Go, but it has alternate forms. // For ist die einzige Schleifenform in Go, sie hat aber mehrere Formen:
for { // infinite loop for { // Endloschleife
break // just kidding break // nur ein Spaß
continue // unreached continue // wird nie ausgeführt
} }
// As with for, := in an if statement means to declare and assign y first,
// then test y > x. // Wie bei for, bedeutet := in einer Bedingten Anweisung zunächst die
// Zuweisung und erst dann die Überprüfung der Bedingung.
if y := expensiveComputation(); y > x { if y := expensiveComputation(); y > x {
x = y x = y
} }
// Function literals are closures. // Funktionsliterale sind "closures"
xBig := func() bool { xBig := func() bool {
return x > 100 // references x declared above switch statement. return x > 100 // Verweist auf x, deklariert vor dem switch
} }
fmt.Println("xBig:", xBig()) // true (we last assigned 1e6 to x) fmt.Println("xBig:", xBig()) // true (im moment gilt: x == 1e6)
x /= 1e5 // this makes it == 10 x /= 1e5 // dies macht x == 10
fmt.Println("xBig:", xBig()) // false now fmt.Println("xBig:", xBig()) // jetzt: false
// When you need it, you'll love it. // Wenn Sie's brauchen, werden Sie's lieben!
goto love goto love
love: love:
learnInterfaces() // Good stuff coming up! learnInterfaces() // Jetzt zum interessanten Teil!
} }
// Define Stringer as an interface type with one method, String. // Define Stringer as an interface type with one method, String.