Question: What are pointers in Go, and how are they used?
|
Answer: The pointers in Go are variables that store memory addresses of other variables. They are used to
indirectly refer to a value stored in memory. Pointers are commonly used to pass references to data structures,
avoid unnecessary copying of large data, and enable efficient memory management.
|
Question: Can you explain the concept of concurrency vs. parallelism in Go?
|
Answer: In Go concurrency refers to the ability of a program to handle multiple tasks simultaneously,
making progress on each task through interleaved execution.
Parallelism, on the other hand, involves actually executing multiple tasks simultaneously across multiple CPUs or CPU cores.
Go supports both concurrency and parallelism through goroutines and can utilize multiple CPU cores to achieve parallel execution.
|
Question: What is the purpose of the 'select' statement in Go?
|
Answer: The 'select' statement in Go is used to choose which of several communication operations will proceed.
It allows a goroutine to wait on multiple communication operations simultaneously, making it useful for implementing multiplexed
network servers, timeouts, and other concurrent patterns.
|
Question: How does Go handle errors compared to exceptions in other programming languages?
|
Answer: Go does not have exceptions like many other programming languages. Instead, errors are treated as normal values
that are returned from functions. This promotes explicit error handling, where functions return an error as one of their
return values, and the caller must check for and handle errors explicitly using conditional statements.
|
Question: What are 'defer', 'panic', and 'recover' used for in Go?
|
Answer: The 'defer' is used to schedule a function call to be executed when the surrounding function returns.
The 'panic' is used to terminate the execution of a function and trigger a panic, which can be caught
by a deferred function using recover.
And 'recover' is used to regain control of a panicking goroutine and resume normal execution,
typically used for error handling in exceptional situations.
|
Question: Can you explain the concept of interfaces in Go with an example?
|
Answer: Interfaces in Go define a set of methods that a type must implement to be considered as implementing that interface.
For example, the io.Writer interface requires a type to implement a Write method that writes data to a stream.
Any type that implements the Write method can be used wherever an io.Writer is expected, enabling polymorphism and code reuse.
|
Question: What is the difference between a 'map' and a 'slice' in Go?
|
Answer: A 'map' in Go is a built-in data structure that maps keys to values, allowing efficient lookup, insertion, and deletion of key-value pairs.
A 'slice', on the other hand, is a flexible and dynamic view into an underlying array, allowing for the manipulation of sequences of elements.
'Maps' are used for key-value mappings, while 'slices' are used for sequences of elements.
|
Question: How does Go support testing?
|
Answer: Go has a built-in testing framework that allows developers to write tests using the testing package.
Test functions must be named with a prefix of Test and take a single parameter of type *testing.T.
Developers can use functions like t.Errorf to report test failures and t.Logf to log messages during test execution.
The go test command is used to run tests in the current package.
|