Go (Golang) language interview questions and answers for freshers and experienced.
50 Go (Golang) interview questions along with brief answers. These questions cover a wide range of topics including basic syntax, concurrency, error handling, and more.
Basic Go (Golang) Questions
What is Go?
Go (or Golang) is an open-source programming language developed by Google. It is designed for concurrency, simplicity, and efficiency.
What are some key features of Go?
Go has garbage collection, concurrency support via goroutines, static typing, built-in libraries, and a simple syntax.
How do you declare a variable in Go?
var x int = 10 or x := 10 (short form).
What are the different ways to declare a variable in Go?
Using var, :=, and the const keyword for constants.
What is the difference between var and := in Go?
var is used for explicit declarations, while := is shorthand for implicit declaration within functions.
What is a package in Go?
A package is a way to group Go files. Every Go program must have a main package as the entry point.
How do you import a package in Go?
import "package_name"
What is a blank identifier (_) in Go?
It's used to ignore a value you don't need in the program.
What are pointers in Go?
A pointer is a variable that holds the memory address of another variable. Declared with * for types and & for obtaining the address.
What is the difference between make and new in Go?
new allocates memory but does not initialize it, while make both allocates and initializes slices, maps, and channels.
Functions and Methods
How do you define a function in Go?
func functionName(parameter type) returnType { // function body }
What is the difference between a method and a function in Go?
A method is a function with a receiver type (associated with a type).
Can Go functions return multiple values?
Yes, Go allows multiple return values: func swap(a, b int) (int, int) { return b, a }.
What is a variadic function in Go?
A function that accepts a variable number of arguments. Example: func sum(nums ...int) int.
How does Go handle error values in functions?
Go does not have exceptions. Instead, functions return error values that should be checked by the caller.
What is a closure in Go?
A closure is a function that captures variables from its surrounding scope.
Control Structures and Loops:
What looping constructs are available in Go?
Go has only the for loop, which can be used like a while or do-while loop.
What are the different forms of for loop in Go?
Basic for, range-based for (for iterating slices, arrays, etc.), and infinite for.
How do you break or continue loops in Go?
Use break to exit and continue to skip the remaining code in the current iteration.
What is the select statement in Go?
select is used for synchronizing on multiple channel operations.
What is the defer statement in Go?
defer is used to schedule a function call to be run after the function completes, often for cleanup.
Can you use if and switch statements in Go without parentheses?
Yes, parentheses are not required, but curly braces are mandatory.
Data Structures
What is a slice in Go?
A slice is a dynamically-sized, flexible view into an array. Slices are more common than arrays in Go.
How do you create a slice in Go?
Using the make function or slicing an array: make([]int, length, capacity) or arr[1:4].
What is the difference between an array and a slice in Go?
Arrays have a fixed size, whereas slices are dynamically resizable.
How do you append elements to a slice in Go?
Using the append function: slice = append(slice, newElement).
What are maps in Go?
Maps are key-value pairs, declared as map[keyType]valueType.
How do you check if a key exists in a map?
By using the syntax val, ok := map[key]. ok will be true if the key exists.
What are structs in Go?
Structs are custom data types that group fields together, similar to objects in other languages.
How do you define a struct in Go?
type Person struct { Name string; Age int }.
Concurrency
What are goroutines in Go?
Goroutines are lightweight threads managed by the Go runtime, created using the go keyword.
What is a channel in Go?
Channels are a way for goroutines to communicate with each other and synchronize execution.
How do you send and receive data on a channel?
Use <- to send (ch <- val) and receive (val := <-ch) data on a channel.
What are buffered channels in Go?
Channels that can hold a fixed number of values before blocking further sends.
What is the purpose of the sync.WaitGroup?
WaitGroup is used to wait for a collection of goroutines to finish executing.
What are the advantages of Go’s concurrency model?
Go’s concurrency model is simple and lightweight, with easy-to-use goroutines and channels for communication.
What is a select statement used for in concurrency?
The select statement lets a goroutine wait on multiple channel operations.
Can a Go channel be closed?
Yes, channels can be closed using the close(channel) function to indicate no more values will be sent.
What happens if a goroutine tries to read from a closed channel?
It receives the zero value of the channel's type.
Error Handling:
How does Go handle errors?
Go uses a convention of returning error values along with the expected return type from functions.
What is the error type in Go?
error is an interface in Go that represents error messages. func (e *myError) Error() string.
What is panic and recover in Go?
panic stops normal execution and recover can catch a panic to regain control of the program.
When should you use panic in Go?
Panic should be used for unrecoverable errors, such as critical program faults.
Go Project Structure and Best Practices
What is Go module?
A Go module is a collection of Go packages stored in a file tree with a go.mod file at its root.
How do you create a new Go module?
By running go mod init module_name.
What is the purpose of the go.mod file?
The go.mod file defines the module’s path and its dependency versions.
How do you run tests in Go?
Go uses the go test command to run tests, typically placed in files with _test.go suffix.
How do you benchmark code in Go?
Go’s testing package includes benchmarks using func BenchmarkXxx(b *testing.B).
What are Go's memory management features?
Go uses garbage collection to manage memory, removing the need for manual memory management.
What is the difference between Go and C/C++?
Go is simpler, has built-in garbage collection, built-in concurrency, and is designed for modern hardware architectures, whereas C/C++ require manual memory management.
No comments:
Post a Comment