Which part of the HTTP request lifecycle is typically modified or intercepted by middleware?
- After the response is sent
- Before the request reaches the handler
- During database operations
- During the rendering of templates
Middleware typically intercepts and modifies the HTTP request before it reaches the handler, allowing for operations such as authentication, logging, and request parsing to take place.
What is the zero value of a struct in Go?
- The struct with all fields initialized to zero values
- The struct with all fields set to 0
- The struct with all fields set to nil
- The struct with no fields initialized
In Go, the zero value of a struct is a struct with all fields initialized to their respective zero values. This means numeric fields are set to 0, strings are set to "", and pointers are set to nil.
Is it possible to resume execution after a 'panic()' in Go?
- No, once a panic occurs, the program terminates
- No, panics cannot be recovered in Go
- Yes, by calling 'recover()' within a deferred function
- Yes, by using a try-catch block
Yes, it's possible to resume execution after a panic in Go by using the 'recover()' function within a deferred function. When a panic occurs, the control transfers to deferred functions, and by calling 'recover()' in one of these deferred functions, the program can continue execution.
Type assertion in Go is performed using the syntax _______.
- .(assert)
- .(assertion)
- .(type)
- .(value)
In Go, type assertion is performed using the syntax .(type), where type is the specific type that you are asserting. This syntax is used to extract the concrete value of the underlying type from an interface value. It helps in accessing the underlying concrete type and its value safely.
What does the json.MarshalIndent function do in Go?
- Deserializes JSON data
- Removes whitespace from JSON strings
- Serializes data into a JSON string with indentation
- Validates JSON data
The json.MarshalIndent function in Go is used to serialize data into a JSON string with indentation for readability. It takes a Go data structure as input and returns a formatted JSON string with each element properly indented according to its nesting level. This function is particularly useful for generating human-readable JSON output, especially when dealing with complex data structures.
In Go, passing a pointer to a function allows _______ to be modified outside the function scope.
- constants
- structures
- values
- variables
Passing a pointer to a function in Go enables variables to be modified outside the function scope. This is because the function receives the memory address of the variable, allowing it to directly manipulate the data stored at that address.
What does the blank identifier (_) represent when used in an import statement in Go?
- It represents a wildcard import, importing all symbols from the package
- It represents an alias for the imported package
- It represents importing the package only for its side effects
- It represents not importing the package at all
In Go, when the blank identifier (_) is used in an import statement like import _ "package", it indicates that the package is imported solely for its side effects, such as initializing global variables or registering functions, without making its symbols directly accessible in the code.
What is the purpose of the sql.NullString type in the database/sql package?
- Encode String Values
- Manage SQL Connections
- Represent Nullable String
- Store SQL Query Results
The purpose of the sql.NullString type in the database/sql package is to represent nullable string values retrieved from a SQL database. It consists of two fields: String and Valid. The String field holds the actual string value, while the Valid field indicates whether the string is valid or null. This type is useful when dealing with database columns that may contain NULL values, providing a way to differentiate between actual string values and null values.
You're writing a library in Go that interacts with external resources like databases and APIs. How would you design error handling in this library to provide useful information to users of the library?
- Expose low-level error codes directly to the library users.
- Implement retries with exponential backoff for all external calls.
- Only return generic error messages to avoid exposing internal details.
- Wrap external errors with additional context to provide more information.
Designing error handling in a Go library that interacts with external resources involves wrapping external errors with additional context to offer meaningful information to users. This helps users understand the nature of errors and aids in debugging. Exposing low-level error codes directly could lead to confusion and unnecessary complexity. Implementing retries with exponential backoff can improve the library's resilience to transient errors.
What is the main difference between authentication and authorization?
- Authentication grants access, while authorization validates identity
- Authentication validates identity, while authorization grants access
- Authentication verifies access rights, while authorization determines identity
- Authentication verifies identity, while authorization determines access rights
Authentication is the process of verifying the identity of a user, while authorization is the process of determining what resources or actions a user is allowed to access or perform after authentication.