Skip to content

Commit ea2ef68

Browse files
talgat-rubytalgat-yandex
authored andcommitted
add lesson3
1 parent b68f15f commit ea2ef68

File tree

9 files changed

+382
-0
lines changed

9 files changed

+382
-0
lines changed

lesson3/LECTURE.md

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
# Lesson 3: Basic operations
2+
3+
## Block
4+
5+
Each place where a declaration occurs is called a block. Variables, constants, types,
6+
and functions declared outside any functions are placed in the package block. Within a function,
7+
every set of braces ({}) defines another block,
8+
and in a bit you will see that the control structures in Go define blocks of their own.
9+
10+
You can access an identifier defined in any outer block from within any inner block.
11+
This raises the question:
12+
what happens when you have a declaration with the same name as an identifier in a containing block?
13+
If you do that, you _shadow_ the identifier created in the outer block.
14+
15+
## Shadowing Variables
16+
17+
```go
18+
func main() {
19+
x := 10
20+
if x > 5 {
21+
fmt.Println(x) // 10
22+
x := 5
23+
fmt.Println(x) // 5
24+
}
25+
fmt.Println(x) // 10
26+
}
27+
```
28+
29+
**!example1**
30+
31+
A _shadowing variable_ is a variable that has the same name as a variable in a containing block.
32+
For as long as the shadowing variable exists, you cannot access a shadowed variable.
33+
34+
You can also shadow the package or default functions:
35+
36+
```go
37+
package main
38+
39+
import (
40+
"fmt"
41+
)
42+
43+
func main() {
44+
fmt.Println("hello")
45+
fmt := "oops"
46+
fmt.Println("hello")
47+
}
48+
```
49+
50+
```shell
51+
$ go run ./...
52+
./main.go:10:6: fmt.Println undefined (type string has no field or method Println)
53+
```
54+
55+
## If, else if, else
56+
57+
The `if` statement in Go is much like the `if` statement in most programming languages.
58+
59+
```go
60+
n := rand.Intn(10)
61+
if n < 5 {
62+
fmt.Println("That's too low:", n)
63+
} else if n > 5 {
64+
fmt.Println("That's too big:", n)
65+
} else {
66+
fmt.Println("Perfect:", n)
67+
}
68+
```
69+
70+
Go adds is the ability to declare variables that are scoped to the condition and to both the `if` and `else` blocks.
71+
72+
```go
73+
if n := rand.Intn(10); n < 5 {
74+
fmt.Println("That's too low:", n)
75+
} else if n > 5 {
76+
fmt.Println("That's too big:", n)
77+
} else {
78+
fmt.Println("Perfect:", n)
79+
}
80+
```
81+
82+
## Loop
83+
84+
* C-style for with open parts
85+
```go
86+
for i := 0; i < 10; i++ {
87+
fmt.Println(i)
88+
}
89+
```
90+
```go
91+
i := 0
92+
for ; i < 10; i++ {
93+
fmt.Println(i)
94+
}
95+
```
96+
```go
97+
for i := 0; i < 10; {
98+
fmt.Println(i)
99+
if i % 2 == 0 {
100+
i++
101+
} else {
102+
i+=2
103+
}
104+
}
105+
```
106+
```go
107+
i := 1
108+
for i < 100 {
109+
fmt.Println(i)
110+
i=i*2
111+
}
112+
```
113+
```go
114+
for {
115+
fmt.Println("Hello")
116+
}
117+
```
118+
* for-range
119+
120+
```go
121+
fibNums := []int{1, 1, 2, 3, 5, 8}
122+
for i, v := range fibNums {
123+
fmt.Println(i, v)
124+
}
125+
```
126+
127+
**!example2**
128+
129+
### break and continue
130+
131+
`break` exits the loop immediately, just like the `break` statement in other languages
132+
133+
```go
134+
i := 0
135+
for {
136+
if i == 5 {
137+
break
138+
}
139+
i++
140+
}
141+
fmt.Println(i)
142+
```
143+
144+
`continue` keyword, which skips over the rest of the for loop’s body and proceeds directly to the next iteration
145+
146+
```go
147+
for i := 1; i <= 100; i++ {
148+
if i%3 == 0 {
149+
if i%5 == 0 {
150+
fmt.Println("FizzBuzz")
151+
} else {
152+
fmt.Println("Fizz")
153+
}
154+
} else if i%5 == 0 {
155+
fmt.Println("Buzz")
156+
} else {
157+
fmt.Println(i)
158+
}
159+
}
160+
```
161+
162+
But it is not idiomatic
163+
164+
```go
165+
for i := 1; i <= 100; i++ {
166+
if i%3 == 0 && i%5 == 0 {
167+
fmt.Println("FizzBuzz")
168+
continue
169+
}
170+
if i%3 == 0 {
171+
fmt.Println("Fizz")
172+
continue
173+
}
174+
if i%5 == 0 {
175+
fmt.Println("Buzz")
176+
continue
177+
}
178+
fmt.Println(i)
179+
}
180+
```
181+
182+
### for-range value is a copy
183+
184+
**!example3**
185+
186+
## Labels and goto
187+
188+
**!example4**
189+
190+
## switch
191+
192+
Most developers in those languages avoid `switch` statements because of their limitations on values
193+
that can be switched on and the default fall-through behavior. But Go is different.
194+
It makes `switch` statements useful.
195+
196+
```go
197+
words := []string{"a", "cow", "smile", "gopher", "octopus", "anthropologist"}
198+
for _, word := range words {
199+
switch size := len(word) {
200+
case size < 5:
201+
fmt.Println(word, "is a short word!")
202+
case 5:
203+
wordLen := len(word)
204+
fmt.Println(word, "is exactly the right length:", wordLen)
205+
case size > 10:
206+
default:
207+
fmt.Println(word, "is a long word!")
208+
}
209+
}
210+
```
211+
212+
```go
213+
words := []string{"hi", "salutations", "hello"}
214+
for _, word := range words {
215+
switch wordLen := len(word); {
216+
case wordLen < 5:
217+
fmt.Println(word, "is a short word!")
218+
case wordLen > 10:
219+
fmt.Println(word, "is a long word!")
220+
default:
221+
fmt.Println(word, "is exactly the right length.")
222+
}
223+
}
224+
```
225+
226+
!fallthrough
227+
228+
## Exercises
229+
230+
### Exercise 1
231+
232+
Write a for loop that puts 100 random numbers between 0 and 100 into a slice and prints it.
233+
234+
### Exercise 2
235+
236+
Loop over the slice you created in exercise 1. For each value in the slice, apply the following rules:
237+
1. If the value is divisible by 2, print “Two!”
238+
2. If the value is divisible by 3, print “Three!”
239+
3. If the value is divisible by 2 and 3, print “Six!”. Don’t print anything else.
240+
4. Otherwise, print “Never mind”.
241+
242+
## Exercise 3
243+
244+
Start a new program. In main, declare an `int` variable called `total`.
245+
Write a `for` loop that uses a variable named `i` to iterate from `0` (inclusive) to `10` (exclusive).
246+
The body of the for loop should be as follows:
247+
```go
248+
total := total + i
249+
fmt.Println(total)
250+
```
251+
After the for loop, print out the value of `total`.

lesson3/example1/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/talgat-ruby/lessons-go/lesson3/example1
2+
3+
go 1.22.5

lesson3/example1/main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
)
7+
8+
// NOTE shadow true
9+
10+
func main() {
11+
//slog.Info("Hello")
12+
//fmt.Println(slog)
13+
//slog := "oops"
14+
//fmt.Println(slog)
15+
16+
//fmt.Println(true)
17+
//true := 42
18+
//fmt.Println(true)
19+
20+
n := rand.Intn(10)
21+
22+
if n < 5 {
23+
fmt.Println("That's too low:", n)
24+
return
25+
}
26+
27+
if n > 5 {
28+
fmt.Println("That's too big:", n)
29+
return
30+
}
31+
32+
fmt.Println("Perfect:", n)
33+
}

lesson3/example2/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/talgat-ruby/lessons-go/lesson3/example2
2+
3+
go 1.22.5

lesson3/example2/main.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
fibNums := []int{1, 1, 2, 3, 5, 8}
9+
for i, v := range fibNums {
10+
fmt.Println(i, v)
11+
}
12+
13+
simpsons := map[string]string{
14+
"father": "Homer",
15+
"mother": "Marge",
16+
"son": "Bart",
17+
"daughter1": "Liza",
18+
"daughter2": "Maggie",
19+
}
20+
for role, name := range simpsons {
21+
fmt.Println(role, name)
22+
}
23+
24+
samples := []string{"Batman", "J🤪ker", "apple_π!"}
25+
for _, sample := range samples {
26+
for i, r := range sample {
27+
fmt.Println(i, r, string(r))
28+
}
29+
}
30+
31+
for i := range 10 {
32+
fmt.Println(i) // 0 1 2 3 4 5 6 7 8 9
33+
}
34+
}

lesson3/example3/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/talgat-ruby/lessons-go/lesson3/example3
2+
3+
go 1.22.5

lesson3/example3/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
fibNums := []int{1, 1, 2, 3, 5, 8}
9+
for _, v := range fibNums {
10+
v += v
11+
fmt.Println(v)
12+
}
13+
fmt.Println(fibNums)
14+
}

lesson3/example4/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/talgat-ruby/lessons-go/lesson3/example4
2+
3+
go 1.22.5

lesson3/example4/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
withContinue()
9+
withGoto()
10+
}
11+
12+
func withContinue() {
13+
duo := []string{"SpongeBob", "Patrick"}
14+
outer:
15+
for _, character := range duo {
16+
for i, r := range character {
17+
if r == 'B' {
18+
continue outer
19+
}
20+
fmt.Println(i, r, string(r))
21+
}
22+
fmt.Println(character)
23+
}
24+
}
25+
26+
func withGoto() {
27+
a := 10
28+
b := 20
29+
goto skip
30+
skip:
31+
c := 30
32+
fmt.Println(a, b, c)
33+
if c > a {
34+
goto inner
35+
}
36+
inner:
37+
fmt.Println("a is less than a")
38+
}

0 commit comments

Comments
 (0)