Explain how reflection can be used to inspect struct fields in Go.
- Accessing struct fields using reflect.FieldByName() method
- Getting struct field tags using reflection
- Iterating over struct fields using reflection
- Using the reflect.TypeOf() function to get struct field information
Reflection in Go allows for dynamic examination of types at runtime. To inspect struct fields, you can use reflect.TypeOf() function, which returns a Type object containing information about the struct, including its fields.
In Go, can anonymous functions be passed as arguments to other functions?
- Depends on the Context
- No
- Only if Declared Inline
- Yes
Yes, in Go, anonymous functions are first-class citizens, which means they can be assigned to variables, passed as arguments to other functions, and returned from functions. This feature enables powerful functional programming patterns and enhances the expressiveness of the language.
In Go, '_______' is a built-in function that stops the ordinary flow of control and begins panicking.
- defer
- halt
- panic
- recover
The panic function in Go stops the ordinary flow of control and begins panicking. It's often used to indicate unexpected errors or conditions in the program.
What is the significance of using the 'testing.Benchmark' function in Go?
- To execute a series of test cases
- To generate sample data for testing
- To measure the performance of a piece of code
- To mock external dependencies
The 'testing.Benchmark' function in Go is used to measure the performance of a piece of code. It allows developers to run benchmarks on functions to understand their execution time and performance characteristics. By utilizing this function, developers can optimize critical parts of their codebase for better efficiency.
Go provides a tool named _________ to manage dependencies and versioning.
- dep
- go.mod
- godep
- goget
In Go, the tool named go.mod is used to manage dependencies and versioning. It's a central part of Go's dependency management system introduced in Go 1.11. The go.mod file defines the module's name, its dependencies with their specific versions, and other module-specific metadata. This file is automatically created when you initialize a new Go module in your project using the go mod init command.
Which command is used to run benchmarks in Go?
- go bench
- go benchmark
- go run -bench
- go test -bench
The command used to run benchmarks in Go is go test -bench. This command executes any benchmark functions in the test files of the current package. It measures the performance of these benchmark functions and displays the results.
What does the import keyword do in Go?
- Declares a function
- Defines a new variable
- Imports a package
- Includes a file
The import keyword in Go is used to include packages from the Go standard library or third-party packages. It allows you to use functions, types, and variables defined in those packages within your program.
Explain the concept of "two-phase commit" in the context of distributed transactions.
- Atomicity guarantee
- Data replication
- Distributed consensus
- Failure recovery
The "two-phase commit" protocol ensures atomicity and consistency in distributed transactions. In the first phase, a coordinator node sends a prepare message to all participating nodes, asking if they are ready to commit. If all nodes respond affirmatively, in the second phase, the coordinator sends a commit message, and all nodes execute the transaction. If any node fails to respond or votes against committing, the coordinator sends an abort message, and all nodes roll back the transaction, ensuring consistency across the distributed system.
In Go, what function is used to start an HTTP server?
- http.Serve
- http.StartServer
- http.ListenAndServe
- http.Handle
The correct option is http.ListenAndServe. This function starts an HTTP server and accepts incoming connections on the specified address. It takes a network address as its parameter and a handler to handle requests.
Reflection in Go comes with _______ performance overhead compared to direct method calls.
- moderate
- negligible
- no
- significant
Reflection in Go comes with significant performance overhead compared to direct method calls. This is because reflection involves runtime type introspection and dynamic dispatch, which incur additional runtime cost compared to statically typed languages or direct method calls where the compiler can optimize the code more aggressively. Therefore, while reflection offers flexibility, it should be used judiciously, especially in performance-critical code paths.