Pointers Flashcards

1
Q

What is a pointer in Go?

A

A pointer stores the memory address of another variable and holds a number representing a memory location. Its zero value is nil.

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

What are the & and * operators used for in Go pointers?

A
  • &: Address operator, gives the memory address of a value.
  • *: Indirection operator, dereferences a pointer to get the value at the address.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the syntax for defining a pointer type in Go?

A

A pointer type is written with * before the type name, e.g., *int for a pointer to an integer.

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

What does the built-in new function do in Go?

A

It creates a pointer to a zero-initialized variable of a given type. However, it is rarely used in practice.

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

Why can’t you directly assign a string literal to a pointer field in a struct?

A

Go does not automatically take the address of a string literal. Instead, a helper function like makePointer must be used.

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

How does Go handle function arguments—by value or by reference?

A

Go is call-by-value. Non-pointer types are copied, while pointer types pass a copy of the pointer, allowing modification of the original data.

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

Why are pointers considered a last resort in Go?

A

They increase the workload of the garbage collector, making memory management less efficient.

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

What is the difference between a zero value and no value in Go pointers?

A

A nil pointer represents an uninitialized variable, whereas a zero value indicates an assigned but empty state.

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

Why are maps in Go inherently mutable when passed to functions?

A

Because passing a map to a function passes a copy of the internal pointer, allowing modifications to reflect in the original.

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

How do slices behave when passed to functions?

A

Modifying elements in a slice affects the original, but appending creates a new slice if capacity is exceeded.

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

What is an idiomatic way to use slices in Go?

A

Avoid unnecessary allocations and use slices as buffers to minimize garbage collection.

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

What fields does a slice struct contain in Go?

A

A slice has three fields:

  1. A pointer to the memory block
  2. A length
  3. A capacity
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you reduce the workload on Go’s garbage collector?

A

Use pointers sparingly and avoid unnecessary memory allocations.

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