Fundamentals Flashcards
What is the default value of an uninitialized variable in Go?
The zero value, which depends on the type (e.g., 0 for integers, “” for strings, false for booleans).
How do you declare a variable in Go without initializing it?
var x int (this sets x to the zero value of int, which is 0).
What is the difference between := and var in Go?
:= is used for short variable declarations and can only be used inside functions, while var can be used both inside and outside functions.
What does the defer keyword do in Go?
It defers the execution of a function until the surrounding function returns.
Explain how slices work in Go.
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 do you handle errors in Go?
Errors are handled using a return value, typically the last value returned by a function, and are checked with an if err != nil block.
What is a Goroutine?
A Goroutine is a lightweight thread managed by the Go runtime, allowing concurrent execution of functions.
How do you start a new Goroutine?
By prefixing a function call with the go keyword, e.g., go myFunction().
How does Go manage memory?
Go has automatic memory management with garbage collection, freeing memory that is no longer referenced.
What is a channel in Go?
A channel is a typed conduit through which Goroutines communicate with each other. Channels synchronize Goroutines’ execution.
How do you send data to a channel?
Using the syntax ch <- value, where ch is the channel and value is the data to send.
What is the purpose of the select statement in Go?
select allows a Goroutine to wait on multiple communication operations on channels and execute whichever is ready first.
Can Go support inheritance?
No, Go does not support traditional inheritance but achieves similar behavior through composition using interfaces and structs.
How are interfaces defined in Go?
Interfaces are defined as a set of method signatures. Any type that implements these methods satisfies the interface.
What is a pointer in Go, and how do you declare one?
A pointer holds the memory address of a variable. It is declared with an asterisk (*), e.g., var p *int.
How do you retrieve the value a pointer is pointing to in Go?
By dereferencing the pointer using the asterisk (*), e.g., *p retrieves the value stored at the memory address p points to.
What is the difference between a slice and an array in Go?
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 do you create a slice from an array in Go?
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 do you append elements to a slice in Go?
Using the built-in append function, e.g., slice = append(slice, elem)
What are Go’s built-in data types?
Basic types include int, float64, string, bool, and composite types include array, slice, struct, map, interface, and channel.