In Go, the ________ interface allows you to define custom JSON marshaling and unmarshaling logic.
- CustomJSON
- JSONCustom
- JSONable
- Marshaler
The correct interface in Go that allows you to define custom JSON marshaling and unmarshaling logic is the json.Marshaler and json.Unmarshaler interfaces. By implementing these interfaces for a custom type, you can control how instances of that type are converted to JSON when marshaling and how JSON data is converted back to instances of that type when unmarshaling. This customization is useful when you need to handle special cases or non-standard JSON formatting requirements.
Suppose you're designing a system where multiple goroutines need to communicate with each other asynchronously. Which concurrency construct in Go would you use and why?
- Use a sync.Mutex to achieve mutual exclusion
- Use atomic operations for shared memory access
- Use channels due to their simplicity, safety, and built-in synchronization
- Use semaphores for resource synchronization
Channels are a preferred choice for inter-goroutine communication in Go due to their safety features and built-in synchronization, simplifying the development process.
A method with a _______ receiver in Go can modify the fields of the receiver struct.
- Function
- Interface
- Pointer
- Value
In Go, methods with pointer receivers can modify the fields of the receiver struct, while methods with value receivers cannot. This is because methods with pointer receivers operate on the actual instance of the struct rather than a copy.
The _______ method in the database/sql package is used to execute a SQL query that returns rows.
- Exec
- Query
- Prepare
- Close
The correct option is Query. The Query method in database/sql package is used to execute a SQL query that returns rows. This method is used when you expect rows to be returned from the query execution.
Go interfaces promote _______ programming by allowing objects to interact without knowing their concrete types.
- Dynamic
- Generic
- Polymorphic
- Static
Polymorphic programming is promoted by Go interfaces, enabling different types to be treated uniformly through shared interfaces. This encourages code reuse, flexibility, and adaptability by allowing functions and methods to accept a wide range of types as arguments without the need for explicit type declarations.
The '_____' operator in Go is used to perform bit clear (AND NOT).
- &
- &^
- <
- ^
The correct operator to perform bit clear (AND NOT) in Go is the '&' operator followed by the '^' symbol, forming '&^'. This operator clears bits in the first operand based on the corresponding bits in the second operand.
The _______ data type in Go is used to represent a sequence of bytes.
- array
- bytes.Buffer
- slice
- string
In Go, the _______ data type is represented by the string type. A string is a sequence of bytes, commonly used for representing text data. It is immutable and UTF-8 encoded, allowing for efficient handling of text in various languages.
The _______ function in Go is used to panic, terminating the program immediately with a runtime error message.
- defer
- error
- panic
- recover
The "panic" function in Go is used to cause the program to terminate immediately with a runtime error message. This function is typically called when a critical error occurs that cannot be gracefully handled, leading to an abrupt termination of the program.
The _______ keyword in Go is used to declare anonymous functions.
- func
- defer
- go
- anonymous
In Go, anonymous functions are declared using the func keyword followed by an optional parameter list and return type. Thus, the correct option is 'anonymous'.
What is the purpose of using the 'testing' package's 't.Parallel()' method in Go tests?
- Allows parallel execution of tests
- Ensures sequential execution of tests
- Provides debugging information
- Skips the test execution
In Go, the 't.Parallel()' method in the 'testing' package is used to allow parallel execution of tests. When a test function calls 't.Parallel()', it informs the testing framework that this test can be run in parallel with other tests. This can help in speeding up the test execution time, especially when dealing with I/O-bound or CPU-bound tests. It's essential to ensure that the test functions are safe for parallel execution to avoid race conditions or other concurrency issues.
In Go, can variables be declared without being initialized?
- No
- Rarely
- Sometimes
- Yes
Yes, in Go, variables can be declared without being initialized. When a variable is declared without an explicit initialization, it's assigned the zero value of its type. This is one of the key features of Go's memory safety, ensuring variables always have a valid value.
What are some common pitfalls to avoid when working with JSON in Go?
- Ignoring JSON field tags
- Ignoring error handling when encoding or decoding JSON
- Not considering struct field visibility for JSON encoding
- Using map[string]interface{} excessively
When working with JSON in Go, it's important to avoid common pitfalls to ensure robust and maintainable code. One such pitfall is ignoring JSON field tags, which are used to specify custom field names, omit fields, or handle field visibility during encoding and decoding. Failure to use JSON tags properly can lead to unexpected behavior or incorrect JSON output. Other common pitfalls include ignoring error handling when encoding or decoding JSON, using map[string]interface{} excessively instead of defining appropriate structs, and not considering struct field visibility, which can affect JSON encoding. By being aware of these pitfalls and following best practices, developers can write cleaner and more reliable JSON code in Go.