Go Interview Questions and Answers:
- What is Go?
- Go is an open-source programming language developed by Google. It’s statically typed and has a syntax similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency.
- What are the key features of Go?
- Key features of Go include simplicity, concurrency support through goroutines and channels, fast compilation, garbage collection, and built-in support for networking.
- Explain goroutines in Go.
- Goroutines are lightweight threads managed by the Go runtime. They enable concurrent execution of functions or methods. Goroutines are more efficient than traditional threads and are multiplexed onto fewer OS threads.
- What are channels in Go?
- Channels are a built-in feature of Go used for communication and synchronization between goroutines. They allow safe passing of data between goroutines.
- What is a goroutine leak, and how can you prevent it?
- A goroutine leak occurs when goroutines are created but never closed, leading to excessive memory consumption. You can prevent goroutine leaks by ensuring that goroutines are properly closed when they’re no longer needed, usually using
defer
or synchronization primitives.
- A goroutine leak occurs when goroutines are created but never closed, leading to excessive memory consumption. You can prevent goroutine leaks by ensuring that goroutines are properly closed when they’re no longer needed, usually using
- Explain the difference between a goroutine and a thread.
- Goroutines are lightweight compared to traditional threads. They’re managed by the Go runtime and multiplexed onto a smaller number of OS threads. Goroutines have a smaller initial stack size and grow as needed.
- What is defer in Go?
defer
is a keyword in Go used to schedule a function call to be run after the surrounding function returns. It’s often used for cleanup actions such as closing files or unlocking mutexes.
- How does error handling work in Go?
- Go uses explicit error handling using the
error
type. Functions that can produce errors typically return an error value as their last return value. Developers are encouraged to handle errors explicitly using conditionals or helper functions likepanic
orlog
.
- Go uses explicit error handling using the
- What is a pointer in Go?
- A pointer in Go is a variable that stores the memory address of another variable. It allows indirect access to the value of the variable it points to.
- How do you declare a pointer in Go?
- You declare a pointer by prefixing the type of the pointed-to variable with an asterisk (
*
). For example,var ptr *int
declares a pointer to an integer.
- You declare a pointer by prefixing the type of the pointed-to variable with an asterisk (
- Explain defer, panic, and recover.
defer
schedules a function call to be run after the surrounding function returns.panic
is a built-in function that stops the ordinary flow of control and begins panicking.recover
is a built-in function to regain control of a panicking goroutine.
- What is the purpose of the init function in Go?
- The
init
function is a special function in Go that’s called automatically before the main function in the same package. It’s typically used for package initialization tasks.
- The
- How do you import packages in Go?
- You import packages using the
import
keyword followed by the package path. For example,import "fmt"
imports the fmt package.
- You import packages using the
- What is the difference between unbuffered and buffered channels?
- Unbuffered channels block sender goroutines until there’s a receiver ready to receive the data. Buffered channels have a fixed-size buffer that allows senders to continue sending data until the buffer is full, after which sending blocks.
- How do you create a slice in Go?
- You create a slice by specifying a low and high bound separated by a colon (
:
) within square brackets. For example,mySlice := myArray[low:high]
.
- You create a slice by specifying a low and high bound separated by a colon (
- Explain the difference between a map and a slice in Go.
- A slice is a dynamically sized, flexible view into an underlying array, whereas a map is an unordered collection of key-value pairs.
- What is the zero value in Go?
- The zero value is the default value assigned to variables that are declared but not explicitly initialized. For example, the zero value of an integer is
0
, and for a string, it’s an empty string""
.
- The zero value is the default value assigned to variables that are declared but not explicitly initialized. For example, the zero value of an integer is
- How do you iterate over a map in Go?
- You iterate over a map using a
for
loop with therange
keyword. Each iteration returns a key-value pair.
- You iterate over a map using a
- What is a method in Go?
- A method in Go is a function associated with a particular type. It’s similar to a function but is called using a specific syntax with a receiver argument.
- How do you define a method on a struct in Go?
- You define a method on a struct by specifying the receiver type before the function name. For example:
func (m *MyStruct) MyMethod() {type MyStruct struct {
// fields
}
// method implementation
} - Explain interfaces in Go.
- Interfaces in Go define a set of methods. A type implicitly satisfies an interface if it implements all the methods defined by that interface.
- What is a type assertion in Go?
- A type assertion in Go is used to extract the underlying value of an interface. It checks whether the underlying type of the interface is of the asserted type and returns the value and a boolean indicating success.
- How do you handle errors returned by functions in Go?
- Errors returned by functions in Go are typically checked using conditional statements. Developers can use multiple return values to return errors along with other values from functions.
- What is the purpose of the
sync
package in Go?- The
sync
package provides basic synchronization primitives such as mutexes, condition variables, and atomic operations for managing concurrent access to shared data.
- The
- How do you concatenate strings in Go?
- You can concatenate strings in Go using the
+
operator or thestrings.Join()
function from thestrings
package.
- You can concatenate strings in Go using the
- What is the purpose of the
fmt
package in Go?- The
fmt
package in Go provides functions for formatting input and output. It’s commonly used for printing to the console, formatting strings, and scanning input.
- The
- Explain how to handle CORS in a Go web server.
- CORS (Cross-Origin Resource Sharing) can be handled in a Go web server by setting appropriate CORS headers in the HTTP response, typically using middleware. Headers like
Access-Control-Allow-Origin
,Access-Control-Allow-Methods
, andAccess-Control-Allow-Headers
need to be set based on the requirements.
- CORS (Cross-Origin Resource Sharing) can be handled in a Go web server by setting appropriate CORS headers in the HTTP response, typically using middleware. Headers like
- What is a pointer receiver in Go?
- A pointer receiver in Go is a method receiver that operates on a pointer to the receiver type. It allows the method to modify the receiver’s value.
- Explain the difference between an array and a slice in Go.
- An array in Go has a fixed size defined at compile time, whereas a slice is a dynamically sized view into an underlying array.