interview-questions

GoLang Interview Questions

1. What’s the difference between a goroutine and an operating system thread?

2. Can constants be computed in Go?

const hours = 7643

const minutes = hours * 60

3. What is the shortest way you can swap 2 variables?

var1, var2 = var2, var1

4. Do you have any preferences for error handling methodologies in Go?

Errors in Go are an interface type, where any type that implements the single Error() method can be considered an error.

Whenever a function has a possibility to go wrong, like a network call or a type conversion, the function should return an error as its last return variable. The caller should check the error value, and any value other than nil is considered an error.

Idiomatic Go developers should prefer guard clauses over if-else chains, especially when handling errors. Errors should also be wrapped in a meaningful way as they are passed up the call stack if appropriate.

5. What is a pointer and when would you use it?

A pointer holds the memory address of a value.

Pointers can be used to:

6. Are channels and maps safe for concurrent access?

Channels are safe for concurrent access, for this reason they have blocking operations. Maps are unsafe for concurrent access and require a locking mechanism like a mutex to be safely used across goroutines.

7. How would you sort a slice of custom structs?

I would build a new type that represents a slice of that struct type. For example:

type fruitSlice[]fruit

type car struct {
   size int
   color string
}

Then I would fulfill the standard library’s sort.Interface:

type Interface interface {
   Len() int
   Less(i, j int) bool
   Swap(i, j int)
}

I would then be able to use the sort.Sort function:

sort.Sort(fruitSlice(cars))

8. Does Go support generic programming?

Ask about the persons view on generic programming.

9. Is nil only valid on pointer types?

Nope! nil is the zero value for pointers, interfaces, maps, slices, channels, and function types. nil represents an uninitialized value.

10. How do you export functions from a package?

Exported function in Go just need to be capitalized.

11. How do we perform inheritance with Golang?

This is a bit of a trick question: there is no inheritance in Golang because it does not support classes.

However, you can mimic inheritance behavior using composition to use an existing struct object to define a starting behavior of a new object. Once the new object is created, functionality can be extended beyond the original struct.

12. What kind of typing does GoLang use? Duck Typing/Structural Typing/Nominal Typing?

GO is a Structural-typed language. Nice article - https://medium.com/higher-order-functions/duck-typing-vs-structural-typing-vs-nominal-typing-e0881860bf10

13. Can you return multiple values from a function?

Yes. A Go function can return multiple values, each separated by commas in the return statement.

14. What are wrapped errors?

Wrapped errors in Go refer to the practice of adding additional context to an existing error before passing it up the call stack. This is particularly useful in error handling and debugging.

Key Points

Example

```go import ( “errors” “fmt” )

func someFunction() error { err := anotherFunction() // assume this returns an error if err != nil { // Wrapping the error with additional context return fmt.Errorf(“failed to execute anotherFunction: %w”, err) } return nil }

// In the caller function err := someFunction() if err != nil { // Checking the specific error type or value if errors.Is(err, someSpecificError) { // handle specific error } }