the basics 2 Flashcards

(15 cards)

1
Q

Conditionals

A

if x > 10 {
fmt.Println(“Large”)
} else if x > 5 {
fmt.Println(“Medium”)
} else {
fmt.Println(“Small”)
}

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

Conditionals shorts

A

if val := compute(); val < 0 {
fmt.Println(“Negative”)
}

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

Functions multiple

A

func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New(“division by zero”)
}
return a / b, nil
}

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

Functions named

A

func calc(a, b int) (sum int, diff int) {
sum = a + b
diff = a - b
return // implicitly returns sum, diff
}

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

Exporting (Public):

A

Uppercase names are exported (e.g., Println, GetUser).
Importing:

go
import (
“fmt” // Standard library
“math/rand” // Subpackage
“github.com/user/pkg” // Third-party
)

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

Package Declaration:

A

package main // Executable packages use main

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

Type Casting Explicit

A

var i int = 42
var f float64 = float64(i) // int → float64

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

Type Inference

A

x := 42 // int
y := 3.14 // float64
z := “hello” // string

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

Arrays fixed

A

var arr [3]int // [0, 0, 0]
arr := [3]int{1, 2, 3} // Initialized

Key Point:
Size is part of the type ([3]int ≠ [4]int).

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

Slices (Dynamic Arrays)

A

slice := []int{1, 2, 3} // No fixed size
slice = append(slice, 4) // [1, 2, 3, 4]
sub := slice[1:3] // [2, 3] (half-open)
Underlying Array:
Slices are references to arrays.

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

Maps (Key-Value Pairs)

A

m := map[string]int{“a”: 1, “b”: 2}
m[“c”] = 3 // Insert
val, exists := m[“d”] // exists=false if missing

Key Rule:
Keys must be comparable (no slices/functions).

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

make() (Pre-Allocation) Slices

A

slice := make([]int, 3, 5) // len=3, cap=5

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

make() (Pre-Allocation) maps

A

m := make(map[string]int, 10) // Pre-allocates space
Use Case: Optimize performance by pre-sizing.

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

Structs

A

Declaration:

type User struct {
Name string
Age int
}

Usage:

u := User{“Alice”, 30}
u.Age = 31 // Mutable

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

Structs Embedded

A

type Admin struct {
User
Permissions []string
}

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