the basics Flashcards

(14 cards)

1
Q

for Loop basic

A

for i := 0; i < 5; i++ {
fmt.Println(i) // 0, 1, 2, 3, 4
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

for Loop while

A

n := 0
for n < 5 { // while-like
n++
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

for loop infinite

A

for {
break // exits immediately
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

range array/slice

A

nums := []int{10, 20, 30}
for i, v := range nums { // index, value
fmt.Println(i, v)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

range map

A

m := map[string]int{“a”: 1, “b”: 2}
for k, v := range m { // key, value
fmt.Println(k, v)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

range String (Runes):

A

for i, r := range “Go🔥” { // i=byte index, r=rune
fmt.Printf(“%d: %c\n”, i, r)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

if Statement

A

if x > 0 {
fmt.Println(“Positive”)
} else if x < 0 {
fmt.Println(“Negative”)
} else {
fmt.Println(“Zero”)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

if Statement short

A

if err := doSomething(); err != nil {
fmt.Println(“Error:”, err)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

switch Statement

A

switch os := runtime.GOOS; os {
case “darwin”:
fmt.Println(“macOS”)
case “linux”:
fmt.Println(“Linux”)
default:
fmt.Printf(“%s\n”, os)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

switch Statement no condition

A

switch {
case x > 0:
fmt.Println(“Positive”)
case x < 0:
fmt.Println(“Negative”)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

returning Errors

A

func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, fmt.Errorf(“cannot divide by zero”)
}
return a / b, nil
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Checking Errors:

A

result, err := divide(10, 0)
if err != nil {
log.Fatal(err)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Panic (Crash Program):

A

panic(“something went terribly wrong”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Recover (Deferred Handler):

A

func safeCall() {
defer func() {
if r := recover(); r != nil {
fmt.Println(“Recovered:”, r)
}
}()
panic(“intentional panic”)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly