How can you specify the number of iterations to run during benchmarking in Go?

  • By setting the B.N field within the benchmark function
  • By setting the B.ReportAllocs field within the benchmark function
  • By using the -bench flag
  • By using the -benchtime flag
In Go, the number of iterations to run during benchmarking can be specified by using the -bench flag followed by the desired number of iterations. This allows developers to control the duration and accuracy of their benchmark tests.

Can constants be of a complex data type, such as slices or structs, in Go?

  • No
  • Only if explicitly declared as 'complex' constants
  • Only if they are part of a package-level constant block
  • Yes
No, constants in Go must be simple types like numbers, strings, or booleans. They cannot be of complex types such as slices or structs. This is because constants must have a fixed value at compile-time, and complex types cannot be represented as fixed values at compile-time.

In Go, which statement is used to import a package that is within the same module?

  • import "./package"
  • import "package"
  • import . "package"
  • import _ "package"
The correct statement to import a package that is within the same module in Go is import . "package". This syntax allows you to refer to the package's exported symbols directly without prefixing them with the package name.

In a microservices architecture, how would you implement centralized authentication and decentralized authorization?

  • Implement OAuth 2.0 for both authentication and authorization in a centralized manner.
  • Implement a centralized identity provider service for authentication and use tokens for authorization across services.
  • Implement separate authentication mechanisms for each microservice and delegate authorization logic to each service individually.
  • Use JWT tokens for both authentication and authorization across microservices.
In a microservices architecture, centralized authentication involves having a single identity provider service responsible for authenticating users across all microservices. Decentralized authorization means that each microservice handles its own access control based on the tokens provided during authentication. This approach ensures that authentication is centralized for consistency while allowing individual services to enforce their own authorization rules.

Which testing framework is included in the standard Go library?

  • go test
  • gunit
  • testify
  • testing
The standard Go library includes a built-in testing framework accessible via the "testing" package. It provides functionalities for writing and running tests without any external dependencies.

In Gorm, the _______ function is used to create a new record in the database.

  • Create
  • Insert
  • New
  • Save
In Gorm, the New function is used to create a new record in the database. This function initializes a new instance of the corresponding model struct without adding it to the database. It's useful when you want to create a new record and populate its fields before saving it to the database. This approach provides more control over the creation process, allowing you to set default values or perform validations before persisting the data. Using New followed by Save or Create allows you to manage the creation and persistence of database records efficiently.

What are the differences between slices and arrays in Go in terms of memory allocation?

  • Slices and arrays both are dynamic.
  • Slices and arrays both have fixed size.
  • Slices are dynamic whereas arrays are fixed size.
  • Slices are fixed size whereas arrays are dynamic.
Slices in Go are references to arrays, which means that a slice doesn't store any data itself. Instead, it stores a reference to the underlying array. Unlike arrays, slices can grow or shrink dynamically. This is because slices have a dynamic size, whereas arrays have a fixed size that cannot be changed after declaration.

_______ is a common approach to organize and group related tests in Go.

  • Functions
  • Interfaces
  • Packages
  • Test Suites
In Go, organizing and grouping related tests is commonly achieved using test suites. These allow developers to logically group tests for better management and organization.

Can you have duplicate keys in a map in Go?

  • It depends
  • No
  • Sometimes
  • Yes
No, you cannot have duplicate keys in a map in Go. Each key must be unique. If you attempt to insert a duplicate key, it will replace the existing value associated with that key. Maps in Go provide a one-to-one relationship between keys and values, ensuring efficient retrieval and storage. Attempting to add a duplicate key will overwrite the existing key-value pair. This is a fundamental property of maps in Go and is important to understand when working with them.

Which interface in the database/sql package is used to represent a database row?

  • DataRow
  • Row
  • RowInterface
  • Rows
The interface used to represent a database row in the database/sql package is Row. This interface provides methods to scan column values from a row into Go variables. It allows fetching values of different data types from a row returned by a database query. Utilizing this interface enables flexible and efficient handling of query results, facilitating data processing and manipulation within Go applications.