Questions and Answers
- What do you need for two functions to be the same type?
- They should share the same signatures, including parameter types and return types.
- They should share the same parameter types but can return different types.
- All functions should be the same type.
- The functions should not be a first class type.
- What does the `len()` function return if passed a UTF-8 encoded string?
- the number of characters
- the number of bytes
- It does not accept string types.
- the number of code points
- Which is _not_ a valid loop construct in Go?
- `do { ... } while i < 5`
- `for _,c := range "hello" { ... }`
- `for i := 1; i < 5; i++ { ... }`
- `for i < 5 { ... }`
- How will you add the number 3 to the right side?
- `values.append(3)`
- `values.insert(3, 3)`
- `append(values, 3)`
- `values = append(values, 3)`
- What is the value of `Read`?
- 0
- 1
- 2
- a random value
- Which is the _only_ valid import statement in Go?
- `import "github/gin-gonic/gin"`
- `import "https://github.com/gin-gonic/gin"`
- `import "../template"`
- `import "github.com/gin-gonic/gin"`
- What would happen if you attempted to compile and run this Go program?
- It would not compile because `GlobalFlag` was never initialized.
- It would compile and print `[]`.
- It would compile and print nothing because `"[" +nil+"]"` is also `nil`.
- It would compile but then panic because `GlobalFlag` was never initialized.
- 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`?
- It can be accessed anywhere inside `myPackage`, not the rest of myModule.
- It can be accessed by any application that imports `myModule`.
- It can be accessed from anywhere in `myModule`.
- It can be accessed by other packages in `myModule` as long as they import `myPackage`
- How do you tell `go test` to print out the tests it is running?
- `go test`
- `go test -x`
- `go test --verbose`
- `go test -v`
- This code printed `{0, 0}`. How can you fix it?
- use `json.Decoder`
- Pass a pointer to `data`
- Make `X` and `Y` exported (uppercase)
- Use field tags
- What does a `sync.Mutex` block while it is locked?
- all goroutines
- any other call to lock that `Mutex`
- any reads or writes of the variable it is locking
- any writes to the variable it is locking
- What is an idiomatic way to pause execution of the current scope until an arbitrary number of goroutines have returned?
- Pass an `int` and `Mutex` to each and count when they return.
- Loop over a `select` statement.
- Sleep for a safe amount of time.
- `sync.WaitGroup`
- What is a side effect of using `time.After` in a `select` statement?
- It blocks the other channels.
- It is meant to be used in select statements without side effects.
- It blocks the `select` statement until the time has passed.
- The goroutine does not end until the time passes.
- What is the select statement used for?
- executing a function concurrently
- executing a different case based on the type of a variable
- executing a different case based on the value of a variable
- executing a different case based on which channel returns first
- According to the Go documentation standard, how should you document this function?
- A
- B
- C
- D
- What restriction is there on the type of `var` to compile this `i := myVal.(int)?`
- `myVal` must be an integer type, such as `int`, `int64`, `int32`, etc.
- `myVal` must be able to be asserted as an `int`.
- `myVal` must be an interface.
- `myVal` must be a numeric type, such as `float64` or `int64`.
- What is a channel?
- a global variable
- a medium for sending values between goroutines
- a dynamic array of values
- a lightweight thread for concurrent programming
- How can you make a file build only on Windows?
- Check runtime.GOOS.
- Add a // +build windows comment anywhere in the file.
- Add a \_ prefix to the file name.
- Add a // +build windows comment at the top of the file.
- What is the correct way to pass this as a body of an HTTP POST request?
- resp, err := http.Post("https://httpbin.org/post", "text/plain", []byte(data))
- resp, err := http.Post("https://httpbin.org/post", "text/plain", data)
- resp, err := http.Post("https://httpbin.org/post", "text/plain", strings.NewReader(data))
- resp, err := http.Post("https://httpbin.org/post", "text/plain", &data)
- What should the idiomatic name be for an interface with a single method and the signature `Save() error`?
- Saveable
- SaveInterface
- ISave
- Saver
- A `switch` statement **\_** its own lexical block. Each `case` statement **\_** an additional lexical block.
- does not create; creates
- does not create; does not create
- creates; creates
- creates; does not create
- What is the default case sensitivity of the JSON `Unmarshal` function?
- The default behavior is case insensitive, but it can be overridden.
- Fields are matched case sensitive.
- Fields are matched case insensitive.
- The default behavior is case sensitive, but it can be overridden.
- What is the difference between the `time` package?s `Time.Sub()` and `Time.Add()` methods?
- Time.Add() is for performing addition while Time.Sub() is for nesting timestamps.
- Time.Add() always returns a later time while time.Sub always returns an earlier time.
- They are opposites. Time.Add(x) is the equivalent of Time.Sub(-x).
- Time.Add() accepts a Duration parameter and returns a Time while Time.Sub() accepts a Time parameter and returns a Duration.
- What is the risk of using multiple field tags in a single struct?
- Every field must have all tags to compile.
- It tightly couples different layers of your application.
- Any tags after the first are ignored.
- Missing tags panic at runtime.
- Where is the built-in recover method useful?
- in the main function
- immediately after a line that might panic
- inside a deferred function
- at the beginning of a function that might panic
- Which choice does _not_ send output to standard error?
- println(message)
- log.New(os.Stderr, "", 0).Println(message)
- fmt.Errorf("%s\n", message)
- fmt.Fprintln(os.Stderr, message)
- How can you tell Go to import a package from a different location?
- Use a proxy.
- Change the import path.
- Use a replace directive in go.mod.
- Use a replace directory.
- If your current working directory is the top level of your project, which command will run all its test packages?
- go test all
- go run --all
- go test .
- go test ./...
- Which encodings can you put in a string?
- any, it accepts arbitary bytes
- any Unicode format
- UTF-8 or ASCII
- UTF-8
- How is the behavior of `t.Fatal` different inside a `t.Run`?
- There is no difference.
- t.Fatal does not crash the test harness, preserving output messages.
- t.Fatal stops execution of the subtest and continues with other test cases.
- t.Fatal stops all tests and contains extra information about the failed subtest.
- What does `log.Fatal` do?
- It raises a panic.
- It prints the log and then raises a panic.
- It prints the log and then safely exits the program.
- It exits the program.
- Which is a valid Go time format literal?
- "2006-01-02"
- "YYYY-mm-dd"
- "y-mo-d"
- "year-month-day"
- How should you log an error (err)
- `log.Error(err)`
- `log.Printf("error: %v", err)`
- `log.Printf(log.ERROR, err)`
- `log.Print("error: %v", err)`
- Which file names will the `go test` command recognize as test files?
- any that starts with `test`
- any files that include the word `test`
- only files in the root directory that end in `_test.go`
- any that ends in `_test.go`
- What will be the output of this code?
- 0
- It will deadlock
- It will not compile
- 2.718
- What will be the output of this program?
- It will deadlock
- It will panic
- 0
- NaN
- What will be printed in this code?
- 0
- 0.000000
- The code will panic
- NaN
- How can you compile main.go to an executable that will run on OSX arm64 ?
- Set GOOS to **arm64** and GOARCH to **darwin**.
- Set GOOS to **osx** and GOARCH to **arm64**.
- Set GOOS to **arm64** and GOARCH to **osx**.
- Set GOOS to **darwin** and GOARCH to **arm64**.
- What is the correct syntax ta start a goroutine that will `print Hello Gopher!`?
- `go(fmt.Println("Hello Gopher!"))`
- `go func() { fmt.Println("Hello Gopher!") }`
- `go fmt.Println("Hello Gopher!")`
- `Go fmt.Println("Hello Gopher!")`
- If you iterate over a map in a for range loop, in which order will the key:value pairs be accessed?
- in pseudo-random order that cannot be predicted
- in reverse order of how they were added, last in first out
- sorted by key in ascending order
- in the order they were added, first in first out
- What is an idiomatic way to customize the representation of a custom struct in a formatted string?
- There is no customizing the string representation of a type.
- Build it in pieces each time by calling individual fields.
- Implement a method `String()` string
- Create a wrapper function that accepts your type and outputs a string.