diff --git a/114-For/for-0.gop b/114-For/for-0.gop new file mode 100644 index 0000000..f511218 --- /dev/null +++ b/114-For/for-0.gop @@ -0,0 +1,2 @@ +# For loop +// Go+ has only one looping keyword: for, with several forms. diff --git a/114-For/for-1.gop b/114-For/for-1.gop new file mode 100644 index 0000000..34e344f --- /dev/null +++ b/114-For/for-1.gop @@ -0,0 +1,26 @@ +# 1、for/<- +// This is the most common form. You can use it with a slice, map, numeric range or custom iterators. +// +// The for value <- arr/map form is used for going through elements of a slice or a map. +// +// If an index is required, an alternative form for index, value <- arr can be used. + +names := ["Sam", "Peter"] +for i, name <- names { + println i, name +} + +m := {"one": 1, "two": 2} +for key, val <- m { + println key, val +} +for key, _ <- m { + println key +} + +for val <- names { + println val +} +for val <- m { + println val +} diff --git a/114-For/for-2.gop b/114-For/for-2.gop new file mode 100644 index 0000000..7e95dc0 --- /dev/null +++ b/114-For/for-2.gop @@ -0,0 +1,14 @@ +# 2、Range for +// You can use range expression (start:end:step) in for loop. + +for i <- :5 { + println i +} + +for i <- 1:5 { + println i +} + +for i <- 1:5:2 { + println i +} diff --git a/114-For/for-3.gop b/114-For/for-3.gop new file mode 100644 index 0000000..b225bf1 --- /dev/null +++ b/114-For/for-3.gop @@ -0,0 +1,11 @@ +# 3、for/<-/if +// All loops of for/<- form can have an optional if condition. + +numbers := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +for num <- numbers if num%3 == 0 { + println num +} + +for num <- :10 if num%3 == 0 { + println num +} diff --git a/114-For/for-4.gop b/114-For/for-4.gop new file mode 100644 index 0000000..e1b5175 --- /dev/null +++ b/114-For/for-4.gop @@ -0,0 +1,16 @@ +# 4、Condition for +// The condition can be omitted, resulting in an infinite loop. You can use break or return to end the loop. + +sum := 0 +i := 1 +for i <= 100 { + sum += i + i++ +} +println sum + +for { + if sum > 0 { + break + } +} diff --git a/114-For/for-5.gop b/114-For/for-5.gop new file mode 100644 index 0000000..37b7e24 --- /dev/null +++ b/114-For/for-5.gop @@ -0,0 +1,9 @@ +# 5、C for +// Finally, there's the traditional C style for loop. It's safer than the while form because with the latter it's easy to forget to update the counter and get stuck in an infinite loop. + +for i := 0; i < 10; i += 2 { + if i == 6 { + continue + } + println i +} diff --git a/114-For/for-6.gop b/114-For/for-6.gop new file mode 100644 index 0000000..873e4ef --- /dev/null +++ b/114-For/for-6.gop @@ -0,0 +1,25 @@ +# break and continue +// How do you get out of an infinite for loop without using the keyboard or +// turning off your computer? That’s the job of the break statement. It exits the +// loop immediately, just like the break statement in other languages. Of course, +// you can use break with any for statement, not just the infinite for statement. +// +// Go+ also includes the continue keyword, which skips over rest of the body of a +// for loop and proceeds directly to the next iteration. Technically, you don’t need +// a continue statement. + +for i := 1; i <= 100; i++ { + if i%3 == 0 && i%5 == 0 { + println("FizzBuzz") + continue + } + if i%3 == 0 { + println("Fizz") + continue + } + if i%5 == 0 { + println("Buzz") + break + } + println(i) +} diff --git a/114-For/for-7.gop b/114-For/for-7.gop new file mode 100644 index 0000000..f02f37a --- /dev/null +++ b/114-For/for-7.gop @@ -0,0 +1,18 @@ +# Labeling Your “for” Statements +// By default, the break and continue keywords apply to the for loop that +// directly contains them. What if you have nested for loops and you want to exit +// or skip over an iterator of an outer loop? Let’s look at an example. We’re going +// to modify our string iterating program to stop iterating through a string as +// soon as it hits a letter “l” + +samples := []string{"hello", "apple_π!"} +outer: + for _, sample := range samples { + for i, r := range sample { + println(i, r, string(r)) + if r == 'l' { + continue outer + } + } + println() + } diff --git a/114-For/for.gop b/114-For/for.gop deleted file mode 100644 index e5f0afb..0000000 --- a/114-For/for.gop +++ /dev/null @@ -1,31 +0,0 @@ -// `for` is Go+'s only looping construct. Here are -// some basic types of `for` loops. - -// The most basic type, with a single condition. -i := 1 -for i <= 3 { - println i - i = i + 1 -} - -// A classic initial/condition/after `for` loop. -for j := 7; j <= 9; j++ { - println j -} - -// `for` without a condition will loop repeatedly -// until you `break` out of the loop or `return` from -// the enclosing function. -for { - println "loop" - break -} - -// You can also `continue` to the next iteration of -// the loop. -for n := 0; n <= 5; n++ { - if n%2 == 0 { - continue - } - println n -} diff --git a/121-For-Each/for-each-1.gop b/121-For-Each/for-each-1.gop new file mode 100644 index 0000000..24ab0f4 --- /dev/null +++ b/121-For-Each/for-each-1.gop @@ -0,0 +1 @@ +// In Go+ there is no foreach loop instead, the for loop can be used as “foreach“. There is a keyword range, you can combine for and range together and have the choice of using the key or value within the loop. diff --git a/121-For-Each/for-each-2.gop b/121-For-Each/for-each-2.gop new file mode 100644 index 0000000..25c6fc1 --- /dev/null +++ b/121-For-Each/for-each-2.gop @@ -0,0 +1,18 @@ +# The for-range Statement +// What makes a for-range loop interesting is that you get two loop variables. +// The first variable is the position in the data structure being iterated, while the +// second is value at that position. The idiomatic names for the two loop variables +// depend on what is being looped over. When looping over an array, slice, or +// string, an i for index is commonly used. When iterating through a map, k (for +// key) is used instead. +// +// The second variable is frequently called v for value, but is sometimes given a +// name based on the type of the values being iterated. +// +// If you don’t need to access the key, use an underscore (_) as the variable’s name. This tells Go+ to ignore the +// value. + +evenVals := []int{2, 4, 6, 8, 10, 12} +for _, v := range evenVals { + println(v) +} diff --git a/121-For-Each/for-each-3.gop b/121-For-Each/for-each-3.gop new file mode 100644 index 0000000..689f08b --- /dev/null +++ b/121-For-Each/for-each-3.gop @@ -0,0 +1,15 @@ +# Iterating Over Maps +// There’s something interesting about how a for-range loop iterates over a +// map. + +m := map[string]int{ + "a": 1, + "c": 3, + "b": 2, +} +for i := 0; i < 3; i++ { + println("Loop", i) + for k, v := range m { + println(k, v) + } +} diff --git a/121-For-Each/for-each.gop b/121-For-Each/for-each.gop deleted file mode 100644 index 8b13789..0000000 --- a/121-For-Each/for-each.gop +++ /dev/null @@ -1 +0,0 @@ -