Fundamentals Flashcards

1
Q

What is the default value of an uninitialized variable in Go?

A

The zero value, which depends on the type (e.g., 0 for integers, “” for strings, false for booleans).

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

How do you declare a variable in Go without initializing it?

A

var x int (this sets x to the zero value of int, which is 0).

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

What is the difference between := and var in Go?

A

:= is used for short variable declarations and can only be used inside functions, while var can be used both inside and outside functions.

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

What does the defer keyword do in Go?

A

It defers the execution of a function until the surrounding function returns.

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

Explain how slices work in Go.

A

A slice is a dynamically-sized, flexible view into the elements of an array. It references a part or all of an underlying array.

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

How do you handle errors in Go?

A

Errors are handled using a return value, typically the last value returned by a function, and are checked with an if err != nil block.

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

What is a Goroutine?

A

A Goroutine is a lightweight thread managed by the Go runtime, allowing concurrent execution of functions.

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

How do you start a new Goroutine?

A

By prefixing a function call with the go keyword, e.g., go myFunction().

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

How does Go manage memory?

A

Go has automatic memory management with garbage collection, freeing memory that is no longer referenced.

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

What is a channel in Go?

A

A channel is a typed conduit through which Goroutines communicate with each other. Channels synchronize Goroutines’ execution.

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

How do you send data to a channel?

A

Using the syntax ch <- value, where ch is the channel and value is the data to send.

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

What is the purpose of the select statement in Go?

A

select allows a Goroutine to wait on multiple communication operations on channels and execute whichever is ready first.

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

Can Go support inheritance?

A

No, Go does not support traditional inheritance but achieves similar behavior through composition using interfaces and structs.

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

How are interfaces defined in Go?

A

Interfaces are defined as a set of method signatures. Any type that implements these methods satisfies the interface.

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

What is a pointer in Go, and how do you declare one?

A

A pointer holds the memory address of a variable. It is declared with an asterisk (*), e.g., var p *int.

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

How do you retrieve the value a pointer is pointing to in Go?

A

By dereferencing the pointer using the asterisk (*), e.g., *p retrieves the value stored at the memory address p points to.

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

What is the difference between a slice and an array in Go?

A

An array has a fixed size defined at declaration, while a slice is a dynamically sized view into an array that can grow or shrink as needed.

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

How do you create a slice from an array in Go?

A

By specifying a range, e.g., arr[1:4] creates a slice from the array arr containing elements at indices 1, 2, and 3.

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

How do you append elements to a slice in Go?

A

Using the built-in append function, e.g., slice = append(slice, elem)

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

What are Go’s built-in data types?

A

Basic types include int, float64, string, bool, and composite types include array, slice, struct, map, interface, and channel.

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

What is the difference between make and new in Go?

A

make is used for initializing slices, maps, and channels, while new allocates memory but does not initialize the value.

22
Q

How do you define a struct in Go?

A

A struct is defined using the type keyword, followed by the struct name and fields.
Example:
~~~
type Person struct {
Name string
Age int
}
~~~

23
Q

How do you instantiate and use a struct in Go?

A
p := Person{Name: "John", Age: 30}  
fmt.Println(p.Name, p.Age)
24
Q

What does the go fmt tool do?

A

go fmt formats Go source code according to the standard style guidelines.

25
Q

What are the ways to declare constants in Go?

A

Constants are declared using the const keyword, and their values must be assigned at compile-time.
~~~
const Pi = 3.14
~~~

26
Q

What is the purpose of the iota keyword in Go?

A

iota is used to simplify constant declarations. It is reset to 0 at the start of each const block and increments automatically.
~~~
const (
A = iota
B = iota
)
~~~

27
Q

What is the purpose of the panic function in Go?

A

panic is used to stop the execution of a program when something unexpected happens. It typically signals unrecoverable errors.

28
Q

How do you recover from a panic in Go?

A

By using the recover function inside a deferred function. recover can catch a panic and prevent the program from crashing.

29
Q

How do you handle multiple return values in Go?

A

Go functions can return multiple values. They are returned as a comma-separated list.
~~~
func divide(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf(“division by zero”)
}
return a / b, nil
}
~~~

30
Q

What is the difference between passing by value and passing by reference in Go?

A

Passing by value creates a copy of the data and does not affect the original variable, while passing by reference (using pointers) allows the function to modify the original variable.

31
Q

How do you create a map in Go?

A

A map is created using the make function or a map literal.
~~~
m := make(map[string]int)
m[“key”] = 10
~~~

32
Q

How do you check if a key exists in a Go map?

A

By using the value-comma-ok idiom.
~~~
val, ok := m[“key”]
if ok {
fmt.Println(“Key exists”)
}
~~~

33
Q

How do you declare a function in Go?

A

Functions are declared using the func keyword followed by the name, parameters, and return type.
~~~
func add(a int, b int) int {
return a + b
}
~~~

34
Q

What is a method in Go, and how is it different from a function?

A

A method is a function with a receiver, which is the type it is associated with.
~~~
func (p Person) Greet() string {
return “Hello, “ + p.Name
}
~~~

35
Q

How do you define an anonymous function in Go?

A

An anonymous function is a function without a name.
~~~
func() {
fmt.Println(“Anonymous function”)
}()
~~~

36
Q

How do you define a function that returns another function in Go?

A

You return the function type as part of the return signature.
~~~
func adder(a int) func(int) int {
return func(b int) int {
return a + b
}
}
~~~

37
Q

What are variadic functions in Go?

A

A variadic function can accept any number of arguments of the same type.
~~~
func sum(nums …int) int {
total := 0
for _, num := range nums {
total += num
}
return total
}
~~~

38
Q

How do you convert a slice into variadic arguments in Go?

A

By using the … operator.
~~~
nums := []int{1, 2, 3}
sum(nums…)
~~~

39
Q

How do you use the range keyword with slices in Go?

A

range is used to iterate over elements in a slice.
~~~
for i, v := range slice {
fmt.Println(i, v)
}
~~~

40
Q

How do you prevent race conditions when working with Goroutines in Go?

A

Use channels or the sync.Mutex to synchronize access to shared variables between Goroutines.

41
Q

What does the sync.WaitGroup do in Go?

A

It is used to wait for a collection of Goroutines to finish executing.
~~~
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
// goroutine logic
}()
wg.Wait()
~~~

42
Q

What are Go’s built-in control flow statements?

A

Go includes standard control flow statements like if, for, switch, and select.

43
Q

How does Go handle switch statements differently than other languages?

A

Go’s switch does not require explicit break statements, as each case is automatically terminated unless the fallthrough keyword is used.

44
Q

What are Go’s looping constructs?

A

Go only has the for loop, which can be used in various ways:

Traditional loop: for i := 0; i < 10; i++ {}
While loop: for i < 10 {}
Infinite loop: for {}

45
Q

What is the difference between break and continue in Go loops?

A

break exits the loop entirely, while continue skips to the next iteration.

46
Q

How do you handle concurrent reads and writes to a map in Go?

A

Go’s maps are not thread-safe, so you should use a sync.Mutex or sync.RWMutex to handle concurrent access to maps.

47
Q

How do you check the type of an interface at runtime in Go?

A

By using a type assertion or a type switch.
~~~
value, ok := interfaceVar.(string)
~~~

48
Q

What does the blank identifier (_) do in Go?

A

The blank identifier is used to discard a value that you don’t need.

49
Q

How do you copy one slice to another in Go?

A

By using the copy function.
copy(dst, src)

50
Q

Can a slice hold different types of data in Go?

A

No, slices in Go are homogenous. They can only hold elements of the same type. However, you can use a slice of type interface{} to store different types.