How do you check for errors when working with files in Go?

  • if err != nil { log.Fatal(err) }
  • if error != nil { panic(error) }
  • if error != nil { return error }
  • if err { return err }
When working with files in Go, you should check for errors by using conditional statements. The correct option is if err != nil { log.Fatal(err) }. This checks if the err variable (commonly used for error handling) is not nil and, if not, logs the error and exits the program using log.Fatal(). Proper error handling is essential when dealing with file operations in Go.

Go has a special statement called defer, which schedules a function to be called _____ the surrounding function returns.

  • after
  • before
  • during
  • instead of
In Go, the defer statement schedules a function to be called after the surrounding function returns. This is often used for tasks like closing files, releasing resources, or ensuring cleanup operations happen even in the presence of errors. When defer is used, the function call is deferred until the end of the enclosing function's scope, ensuring it runs just before that function returns.

Imagine you are building a Go program to manage a library's book inventory. Which data structure would you use to store information about each book and why?

  • Array
  • Map
  • Slice
  • Struct
In this scenario, you would prefer to use a Struct in Go to store information about each book. A Struct allows you to define a custom data type with fields to represent attributes of a book (e.g., title, author, ISBN). It provides a way to encapsulate related data and behaviors into a single unit, making it ideal for representing individual books in the library's inventory. Using a Struct allows you to access book properties using dot notation, making your code more organized and readable.

In Go, to encode a data structure into JSON, the fields in the data structure need to be exported, meaning they need to start with a _____ letter.

  • lowercase
  • uppercase
  • capital
  • special
The correct answer is uppercase. In Go, when you encode a data structure into JSON using the encoding/json package, the fields in the data structure need to be exported, which means they need to start with an uppercase letter. Exported fields are those that are accessible from outside the package, and they are the only fields that the encoding/json package can encode into JSON. This convention is important for proper JSON encoding and decoding in Go.

What are the implications of shadowing in Go?

  • Shadowing can cause variable conflicts.
  • Shadowing can lead to memory leaks.
  • Shadowing can make code harder to read.
  • Shadowing is not allowed in Go.
Shadowing in Go refers to declaring a variable with the same name in an inner scope, which temporarily hides a variable of the same name in an outer scope. While not inherently problematic, it can lead to confusion and potential bugs. When shadowing occurs, it can be challenging to determine which variable is being accessed or modified. It's essential to be aware of shadowing to write clean and maintainable Go code and avoid unexpected behavior caused by variable conflicts.

When a Go interface has many methods, it may be beneficial to _____ it into smaller interfaces for easier mocking.

  • Group
  • Split
  • Combine
  • Expand
When a Go interface becomes large and has many methods, it's often beneficial to split it into smaller interfaces for easier mocking and testing. This practice aligns with the SOLID principles, particularly the "Interface Segregation Principle" (ISP), which recommends that you should have many client-specific interfaces rather than a single, large, general-purpose one. Smaller interfaces make it easier to create focused mocks and test specific interactions.

Explain how you would read from a file in Go line by line.

  • Use the ioutil.ReadFile function to read the entire file into memory at once and then split it into lines.
  • Use the os.Open function to open the file, create a Scanner to read line by line, and loop through the file.
  • Use the fmt.Scanln function to read lines from the file one by one.
  • Use the bufio.NewReader function to create a buffered reader for the file and then use the ReadString method to read lines.
In Go, to read from a file line by line, you typically use the os.Open function to open the file, creating a *os.File object. Then, you create a Scanner using bufio.NewScanner(file) and use a loop to iterate over the lines using the scanner.Scan() method. This method reads one line at a time, and you can access the text of the line using scanner.Text(). This approach is memory-efficient as it doesn't load the entire file into memory.

Explain the concept of a higher-order function in Go.

  • A function that returns an integer.
  • A function that takes a function as an argument and/or returns a function as a result.
  • A function that can be called only from within the same package.
  • A function that cannot be tested.
In Go, a higher-order function is a function that takes one or more functions as arguments and/or returns a function as its result. This concept enables functional programming paradigms in Go, allowing you to write more flexible and reusable code. Higher-order functions are often used to implement functional constructs like map, reduce, and filter, which operate on collections of data. They promote code modularity and make it easier to reason about and test your code.

Describe how to close a channel and why it's important.

  • Use the close() function; it signals no more data.
  • Set the channel to nil to close it.
  • Channels are automatically closed when unused.
  • Closing a channel is not possible in Go.
In Go, you close a channel using the close() function. It's important to close a channel when you're done sending data to it to signal that no more data will be sent. This is crucial for Goroutines waiting on the channel to know that they should stop waiting and exit. Failure to close a channel can lead to deadlocks or Goroutines waiting indefinitely.

In Go, an interface is defined using the _____ keyword.

  • interface{}
  • protocol{}
  • interface
  • implements
In Go, an interface is defined using the interface keyword. Interfaces define a set of methods that a concrete type must implement to satisfy the interface. It is important to note that unlike some other languages, Go interfaces are implicit, meaning that you don't need to explicitly declare that a type implements an interface. Any type that implements the methods defined by an interface is automatically considered to satisfy that interface.