What is the purpose of a JSON Web Token (JWT) in authentication?
- To authenticate and authorize
- To encrypt data during transit
- To provide a digital signature
- To store user session data
The purpose of a JSON Web Token (JWT) in authentication is to authenticate and authorize users. JWTs are used to securely transmit information between parties and can contain claims that assert information about the user, which can be verified and trusted.
What is the zero value of a map in Go?
- 0
- An empty map {}
- The word "map"
- nil
The zero value of a map in Go is an empty map, denoted by {}. This means that if you declare a map variable without initializing it, it will have this empty map as its value. The map will have a zero length and no elements. It's important to note that accessing keys or attempting to range over an uninitialized map will result in a runtime panic. Therefore, it's good practice to always initialize maps before using them to avoid such errors. Understanding the zero value of a map is crucial when working with Go's map data structure.
In Go, a _______ function is a function that can accept one or more functions as arguments or return a function.
- Callback
- Higher-order
- Nested
- Recursive
In Go, a higher-order function is a function that can accept one or more functions as arguments or return a function. This capability allows for more flexible and modular code by enabling functions to be passed as parameters and returned as values.
What are the common strategies for session management in web applications?
- Using JSON Web Tokens (JWT)
- Using URL rewriting
- Using cookies
- Using session tokens
Session management in web applications involves various strategies. Using session tokens is a common approach where a unique token is generated for each session and stored either in a database or in-memory. This token is then validated on subsequent requests to authenticate the user's session.
You're developing a Go application where you need to declare a constant named 'MaxRetryAttempts' to specify the maximum number of retry attempts. Which data type would you use for this constant?
- bool
- float64
- int
- string
In Go, constants can be of various types including numeric constants. Since 'MaxRetryAttempts' is specifying the maximum number of retry attempts, it would typically be represented as an integer. Therefore, you'd use the 'int' data type for this constant.
In database migration, what does the term 'down' typically signify?
- Connecting to a database
- Creating a backup of the database
- Reverting applied migrations to roll back changes
- Updating database records
In database migration, the term 'down' typically signifies reverting applied migrations to roll back changes made to the database schema. This involves executing scripts or commands to undo previous migrations, restoring the database schema to its previous state before the migration was applied.
Using reflection, you can dynamically create _______ and call their methods.
- arrays
- interfaces
- maps
- structs
Using reflection in Go, you can dynamically create interfaces and call their methods. Reflection allows you to inspect and manipulate types, functions, and variables at runtime, which can be useful in scenarios like building generic algorithms or implementing plugins. With reflection, you can create instances of interfaces, invoke their methods, and inspect their properties dynamically.
Which method in the database/sql package is used to close a database connection?
- Close
- Shutdown
- Disconnect
- End
The correct option is Close. In Go's database/sql package, the Close method is used to close a database connection explicitly. It's essential to close connections properly to release resources and prevent leaks.
How are fields accessed in a struct?
- By using arrow notation
- By using bracket notation
- By using dot notation
- By using parenthesis notation
Fields in a struct are accessed using dot notation in Go. This involves typing the name of the struct variable followed by a dot (.) and then the name of the field to access its value.
The _______ keyword in Go is used to define a method on a type.
- func
- method
- struct
- this
In Go, the keyword func is used to define a method on a type. This allows functions to be associated with specific types, enabling a more object-oriented programming style in Go.