Go (Programming Language)

Q1. What do you need for two functions to be the same type?

User defined function types in Go (Golang)

Q2. What does the len() function return if passed a UTF-8 encoded string?

Length of string in Go (Golang).

Q3. Which is not a valid loop construct in Go?

Explanation: Go has only for-loops

Q4. How will you add the number 3 to the right side?

values := []int{1, 1, 2}

Explanation: slices in GO are immutable, so calling append does not modify the slice

Q5. What is the value of Read?

const (
  Write = iota

IOTA in Go (Golang)

Q6. Which is the only valid import statement in Go?

Import in GoLang

Q7. What would happen if you attempted to compile and run this Go program?

package main
var GlobalFlag string
func main() {

variables in Go have initial values. For string type, it’s an empty string.

Go Playground

Q8. From where is the variable myVar accessible if it is declared outside of any functions in a file in package myPackage located inside module myModule?

Explanation: to make the variable available outside of myPackage change the name to MyVar. See also an example of Exported names in the Tour of Go.

Q9. How do you tell go test to print out the tests it is running?

test package

Q10. This code printed {0, 0}. How can you fix it?

type Point struct {
  x int
  y int

func main() {
  data := []byte(`{"x":1, "y": 2}`)
  var p Point
  if err := json.Unmarshal(data, &p); err != nil {
    fmt.Println("error: ", err)
  } else {

How to Parse JSON in Golang?

Go Playground

Q11. What does a sync.Mutex block while it is locked?

Mutex in GoLang

Q12. What is an idiomatic way to pause execution of the current scope until an arbitrary number of goroutines have returned?

Explanation: this is exactly what sync.WaitGroup is designed for - Use sync.WaitGroup in Golang

Q13. What is a side effect of using time.After in a select statement?

Note: it doesn’t block select and does not block other channels.

Q14. What is the select statement used for?

Q15. According to the Go documentation standard, how should you document this function?

func Add(a, b int) {
  return a + b
// Calculate a + b
// - a: int
// - b: int
// - returns: int
func Add(a, b int) {
  return a + b
// Does a + b
func Add(a, b int) {
  return a + b
// Add returns the sum of a and b
func Add(a, b int) {
  return a + b
// returns the sum of a and b
func Add(a, b int) {
  return a + b

Explanation: documentation block should start with a function name

Q16. What restriction is there on the type of var to compile this i := myVal.(int)?

Explanation: This kind of type casting (using .(type)) is used on interfaces only. this example

Primitive types are type-casted differently

Go Playground

Q17. What is a channel?


Q18. How can you make a file build only on Windows?

How to use conditional compilation with the go build tool, Oct 2013

Q19. What is the correct way to pass this as a body of an HTTP POST request?

data := "A group of Owls is called a parliament"


Q20. What should the idiomatic name be for an interface with a single method and the signature Save() error?

Effective Go, Interface names

Q21. A switch statement _ its own lexical block. Each case statement _ an additional lexical block.

Go Language Core technology (Volume one) 1.5-scope

Relevant excerpt from the article:

The second if statement is nested inside the first, so a variable declared in the first if statement is visible to the second if statement. There are similar rules in switch: Each case has its own lexical block in addition to the conditional lexical block.

Q22. What is the default case sensitivity of the JSON Unmarshal function?


Relevant excerpt from the article:

To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match. By default, object keys which don’t have a corresponding struct field are ignored (see Decoder.DisallowUnknownFields for an alternative).

Q23. What is the difference between the time package’s Time.Sub() and Time.Add() methods?



Q24. What is the risk of using multiple field tags in a single struct?

Example Code / b29r0fUD1cp

Q25. Where is the built-in recover method useful?

Example of Recover Function in Go (Golang)

Relevant excerpt from the article:

Recover is useful only when called inside deferred functions. Executing a call to recover inside a deferred function stops the panicking sequence by restoring normal execution and retrieves the error message passed to the panic function call. If recover is called outside the deferred function, it will not stop a panicking sequence.

Q26. Which choice does not send output to standard error?

Q27. How can you tell Go to import a package from a different location?

Q28. If your current working directory is the top level of your project, which command will run all its test packages?

Example of testing in Go (Golang)

Example of cmd in Go (Golang)

Relevant excerpt from the article:

Relative patterns are also allowed, like “go test ./…” to test all subdirectories.

Q29. Which encodings can you put in a string?

Example of encoding in Go (Golang)

Relevant excerpt from the article:

Package encoding defines an interface for character encodings, such as Shift JIS and Windows 1252, that can convert to and from UTF-8.

Q30. How is the behavior of t.Fatal different inside a t.Run?


Fatal is equivalent to Log followed by FailNow.
Log formats its arguments using default formatting, analogous to Println, and records the text in the error log.
FailNow marks the function as having failed and stops its execution by calling runtime.Goexit (which then runs all deferred calls in the current goroutine). Execution will continue at the next test or benchmark. FailNow must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test. Calling FailNow does not stop those other goroutines.
Run runs f as a subtest of t called name. It runs f in a separate goroutine and blocks until f returns or calls t.Parallel to become a parallel test. Run reports whether f succeeded (or at least did not fail before calling t.Parallel).
Run may be called simultaneously from multiple goroutines, but all such calls must return before the outer test function for t returns.

Q31. What does log.Fatal do?

Example of func Fatal in Go (Golang)

Relevant excerpt from the article:

Fatal is equivalent to Print() followed by a call to os.Exit(1).

Q32. Which is a valid Go time format literal?

Example of func Time in Go (Golang)

Relevant excerpt from the article:

Most programs can use one of the defined constants as the layout passed to Format or Parse. The rest of this comment can be ignored unless you are creating a custom layout string.

Q33. How should you log an error (err)

Explanation: There is defined neither log.ERROR, nor log.Error() in log package; log.Print() arguments are handled in the manner of fmt.Print(); log.Printf() arguments are handled in the manner of fmt.Printf().

Q34. How does a sync.Mutex block while it is locked?

Q35. Which file names will the go test command recognize as test files?

Q36. What will be the output of this code?

ch := make(chan int)
ch <- 7
val := <-ch

Q37. What will be the output of this program?

ch := make(chan int)
val := <-ch

Q38. What will be printed in this code?

var stocks map[string]float64 // stock -> price
price := stocks["MSFT"]
fmt.Printf("%f\n", price)

Q39. What is the common way to have several executables in your project?

  1. stackoverflow
  2. medium
  3. medium

Q40. How can you compile main.go to an executable that will run on OSX arm64 ?


Q41. What is the correct syntax ta start a goroutine that will print Hello Gopher!?

Example of start a goroutine

Q42. If you iterate over a map in a for range loop, in which order will the key:value pairs be accessed?


Q43. What is an idiomatic way to customize the representation of a custom struct in a formatted string?