Variables declared with the ':=' syntax in Go are automatically _______ based on the assigned value.

  • inferred
  • initialized
  • typed
  • untyped
In Go, the ':=' syntax is used for short variable declarations. With ':=', Go infers the type of the variable based on the assigned value. This allows for concise variable declaration without explicitly specifying the type.

What is the difference between buffered and unbuffered channels in Go?

  • Buffered channels allow a fixed number of values to be queued before blocking, whereas unbuffered channels block until both sender and receiver are ready.
  • Buffered channels allow concurrent access from multiple goroutines, while unbuffered channels are limited to a single goroutine.
  • Buffered channels are synchronous, while unbuffered channels are asynchronous.
  • Buffered channels have a smaller memory footprint compared to unbuffered channels.
Buffered channels in Go have a fixed size buffer, allowing a certain number of elements to be stored before blocking. On the other hand, unbuffered channels have no capacity for queuing elements, meaning that sends and receives on an unbuffered channel must synchronize immediately. Understanding this difference is crucial for managing communication and synchronization between goroutines efficiently.

The _______ operator in Go returns the memory address of a variable.

  • &
  • *
  • ->
  • .
The ampersand (&) operator in Go is used to retrieve the memory address of a variable. It's crucial for working with pointers and passing by reference.

Which data type in Go is used to represent decimal numbers with floating-point precision?

  • decimal
  • float
  • float32
  • float64
In Go, the float64 data type is used to represent decimal numbers with floating-point precision. It offers a wider range and higher precision compared to float32. The float32 type is also available but is typically used when memory optimization is a concern.

In Go, '_______' can only be called directly by the current goroutine, unlike 'defer'.

  • defer()
  • handle()
  • panic()
  • recover()
In Go, the 'panic()' function is used to cause a runtime panic, which stops normal execution of the current goroutine and begins panicking. Unlike 'defer', 'panic()' cannot be deferred and is limited to the goroutine in which it is called.

What is the difference between 't.Error' and 't.Fatal' in Go unit testing?

  • 't.Error' is used to report a test failure but allows the test to continue execution.
  • 't.Error' stops the execution of the entire test suite.
  • 't.Fatal' is used only for fatal errors unrelated to testing.
  • 't.Fatal' is used to report a test failure and stops the execution of the test immediately.
In Go unit testing, 't.Error' and 't.Fatal' both are used to report test failures. However, 't.Error' allows the test to continue its execution after reporting the error, whereas 't.Fatal' stops the execution of the test immediately upon encountering the error. This subtle difference is crucial, especially when you want to continue testing after encountering an error ('t.Error') or halt further testing upon encountering a critical failure ('t.Fatal').

In Go, which package is commonly used for implementing middleware in web applications?

  • encoding/json
  • fmt
  • net/http
  • os
In Go, the net/http package is commonly used for implementing middleware in web applications. Middleware functions in Go are functions that process incoming HTTP requests before passing them on to the main request handler.

Suppose you're implementing a command-line tool in Go, and you want to handle different options provided by the user. Which control structure in Go would be the most suitable choice for this scenario?

  • if-else
  • switch
  • for-loop
  • select
For handling different options provided by the user in a command-line tool implemented in Go, the switch control structure would be the most suitable choice. The switch statement allows you to evaluate multiple conditions based on the value of an expression and execute the corresponding block of code. This makes it ideal for parsing and handling command-line options efficiently. Unlike if-else statements, which can become cumbersome with multiple options, switch statements offer a cleaner and more concise syntax for managing various cases. Therefore, in this scenario, using switch ensures better readability and maintainability of the code.

What format is commonly used for data interchange in Go?

  • CSV
  • JSON
  • XML
  • YAML
JSON is the commonly used format for data interchange in Go due to its simplicity, readability, and widespread support across various programming languages and systems.

The "." (dot) operator in Go templating refers to the _______ data passed to the template.

  • Context
  • Parameters
  • Scope
  • Variables
In Go templating, the "." (dot) operator is used to access data passed to the template. This data context allows templates to render dynamic content based on the provided data.