Basic Golang (Go) Flashcards

1
Q

Declare a variable of type string. (3 methods)

A

var myStringName string

var myStringName = “Hello there”

myStringName := “Hello there”

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

Declare a variable of type integer (3 methods)

A

var myIntegerName int

var myIntegerName = 5

myIntegerName := 5

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

Declare a variable of type float (3 methods)

A

var myFloatName float32

var myFloatName = 5.05

myFloatName := 5.05

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

Declare a variable of type boolean (3 methods)

A

var isThing bool

isThing bool = true

isThing := true

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

Declare a variable of type rune (3 methods)

A

var someRune rune

someRune rune = “🐻”

someRune := “🐻”

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

What is an anonymous function and when is it typically used?

A

In Go, an anonymous function, also known as a function literal, is a function defined without a name. It can be assigned to a variable, passed as an argument to another function, or executed directly. Anonymous functions are useful for short, one-time operations or when a function is needed only within a specific scope.

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

Using an example, assign a variable a value equal to an anonymous function where the value is “a” plus “b”

A

add := func(a, b int) int {
return a + b
}

result := add(5, 3)

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

A basic function named calcPrice that takes one integer (qty) and returns a float64.

A

func calcPrice(qty int) float64{
return qty * 0.99
}

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

A basic function named calcPrice that takes one integer (qty), two float64 (price and discount) and returns a float64 and boolean.

A

func calcPrice(qty int, price, discount float64) (float64, bool) {
cost := (qty * price) * (1 - discount)
isExpensive := false

return cost, isExpensive }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

A basic struct named “car” with properties of brand, model, and mileage.

A

type car struct {
brand string
model string
mileage int
}

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