What is a closure in the context of anonymous functions?
- A closure can only be defined within a struct
- A closure captures and retains the surrounding state
- A closure is a built-in data type in Go
- A closure is an anonymous function
In Go, a closure is an anonymous function that captures and retains the surrounding state. This means it can access and manipulate variables defined outside of its body. Closures are commonly used in situations where you need to create functions with dynamic behavior that relies on external variables.
When defining a method in Go, which convention is followed for the receiver parameter?
- It is commonly a single letter that represents the type, like 't' for type
- It must always be named 'receiver'
- It must be a pointer to the type
- It must be of interface type
In Go, the convention for the receiver parameter in a method definition is to use a single letter that represents the type, often the first letter of the type's name. This parameter can be either a value or a pointer type, depending on whether the method needs to mutate the receiver.
How do you append elements to a slice in Go?
- add(slice, element)
- append(slice, element)
- extend(slice, element)
- push(slice, element)
In Go, you append elements to a slice using the append function. The syntax is append(slice, element), where slice is the slice you want to append to, and element is the element you want to append.
What keyword is used to define a function in Go?
- def
- func
- function
- procedure
In Go, the keyword "func" is used to define a function. It precedes the function name and parameter list in a function declaration. For example, func functionName(parameters) returnType { ... }.
What happens if you try to send data to a closed channel in Go?
- Compiler error
- Deadlock
- Panic
- Runtime error
Trying to send data to a closed channel in Go results in a panic. This is because sending data to a closed channel is not allowed and will cause a runtime panic, terminating the program.
Which testing framework in Go allows you to write behavior-driven tests using a natural language style?
- GSpec
- GUnit
- Ginkgo
- GoConvey
Ginkgo is a popular testing framework in Go that enables developers to write behavior-driven tests using a natural language style. It provides clear and readable test cases, enhancing the readability of tests.
What is the purpose of the http.HandleFunc function in Go's HTTP server package?
- Registering a handler function for a specific route
- Creating a new HTTP server
- Parsing HTTP requests
- Sending HTTP responses
The correct option is registering a handler function for a specific route. http.HandleFunc is used to associate a handler function with a specific URL pattern. When a request matches the specified pattern, the associated handler function is called to handle the request.
Channels in Go can be _______ or _______ depending on whether they have a buffer.
- Asynchronous, Buffered
- Asynchronous, Unbuffered
- Synchronous, Buffered
- Synchronous, Unbuffered
Channels in Go can be synchronous or asynchronous and buffered or unbuffered. Synchronous channels block the sender until the receiver is ready, while asynchronous channels allow the sender to continue immediately. Buffered channels have a capacity, allowing multiple senders to store data without immediate consumption.
In Go, what is the purpose of the 'errors.New()' function?
- Creating custom error messages
- Handling panics
- Initializing error variables
- Parsing JSON data
In Go, the 'errors.New()' function is used to create custom error messages. This function takes a string as input and returns a new error that implements the 'error' interface. It's commonly used when a specific error message needs to be returned from a function.
The '_______' keyword in Go is used to defer the execution of a function until the surrounding function returns.
- defer
- delay
- panic
- recover
In Go, the defer keyword is used to defer the execution of a function until the surrounding function returns. It's commonly used to ensure cleanup actions are performed, such as closing files or unlocking mutexes.