Go

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

1.

They should share the same signatures, including parameter types and return types.

2.

They should share the same parameter types but can return different types.

3.

All functions should be the same type.

4.

The functions should not be a first class type.

Q 1 / 40

Go

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

1.

the number of characters

2.

the number of bytes

3.

It does not accept string types.

4.

the number of code points

Q 2 / 40

Go

Which is _not_ a valid loop construct in Go?

Explanation: Go has only `for`-loops

1.

`do { ... } while i < 5`

2.

`for _,c := range "hello" { ... }`

3.

`for i := 1; i < 5; i++ { ... }`

4.

`for i < 5 { ... }`

Q 3 / 40

Go

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

1.

`values.append(3)`

2.

`values.insert(3, 3)`

3.

`append(values, 3)`

4.

`values = append(values, 3)`

Q 4 / 40

Go

What is the value of `Read`?

go const ( Write = iota Read Execute )

1.

0

2.

1

3.

2

4.

a random value

Q 5 / 40

Go

Which is the _only_ valid import statement in Go?

1.

`import "github/gin-gonic/gin"`

2.

`import "https://github.com/gin-gonic/gin"`

3.

`import "../template"`

4.

`import "github.com/gin-gonic/gin"`

Q 6 / 40

Go

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

go package main var GlobalFlag string func main() { print("["+GlobalFlag+"]") }

1.

It would not compile because `GlobalFlag` was never initialized.

2.

It would compile and print `[]`.

3.

It would compile and print nothing because `"[" +nil+"]"` is also `nil`.

4.

It would compile but then panic because `GlobalFlag` was never initialized.

Q 7 / 40

Go

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](https://tour.golang.org/basics/3) in the Tour of Go.

1.

It can be accessed anywhere inside `myPackage`, not the rest of myModule.

2.

It can be accessed by any application that imports `myModule`.

3.

It can be accessed from anywhere in `myModule`.

4.

It can be accessed by other packages in `myModule` as long as they import `myPackage`

Q 8 / 40

Go

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

1.

`go test`

2.

`go test -x`

3.

`go test --verbose`

4.

`go test -v`

Q 9 / 40

Go

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

go 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 { fmt.Println(p) } }

1.

use `json.Decoder`

2.

Pass a pointer to `data`

3.

Make `X` and `Y` exported (uppercase)

4.

Use field tags

Q 10 / 40

Go

What does a `sync.Mutex` block white it is locked?

1.

all goroutines

2.

any other call to lock that `Mutex`

3.

any reads or writes of the variable it is locking

4.

any writes to the variable it is locking

Q 11 / 40

Go

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](https://nanxiao.me/en/use-sync-waitgroup-in-golang/)

1.

Pass an `int` and `Mutex` to each and count when they return.

2.

Loop over a `select` statement.

3.

Sleep for a safe amount of time.

4.

`sync.WaitGroup`

Q 12 / 40

Go

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.

1.

It blocks the other channels.

2.

It is meant to be used in select statements without side effects.

3.

It blocks the `select` statement until the time has passed.

4.

The goroutine does not end until the time passes.

Q 13 / 40

Go

What is the select statement used for?

1.

executing a function concurrently

2.

executing a different case based on the type of a variable

3.

executing a different case based on the value of a variable

4.

executing a different case based on which channel returns first

Q 14 / 40

Go

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

go func Add(a, b int) { return a + b } go // Calculate a + b // - a: int // - b: int // - returns: int func Add(a, b int) { return a + b } go // Does a + b func Add(a, b int) { return a + b } go // Add returns the sum of a and b func Add(a, b int) { return a + b } go // returns the sum of a and b func Add(a, b int) { return a + b } Explanation: documentation block should start with a function name

1.

A

2.

B

3.

C

4.

D

Q 15 / 40

Go

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.

1.

`myVal` must be an integer type, such as `int`, `int64`, `int32`, etc.

2.

`myVal` must be able to be asserted as an `int`.

3.

`myVal` must be an interface.

4.

`myVal` must be a numeric type, such as `float64` or `int64`.

Q 16 / 40

Go

What is a channel?

1.

a global variable

2.

a medium for sending values between goroutines

3.

a dynamic array of values

4.

a lightweight thread for concurrent programming

Q 17 / 40

Go

How can you make a file build only on Windows?

1.

Check runtime.GOOS.

2.

Add a // +build windows comment anywhere in the file.

3.

Add a _ prefix to the file name.

4.

Add a // +build windows comment at the top of the file.

Q 18 / 40

Go

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

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

1.

resp, err := http.Post("https://httpbin.org/post", "text/plain", []byte(data))

2.

resp, err := http.Post("https://httpbin.org/post", "text/plain", data)

3.

resp, err := http.Post("https://httpbin.org/post", "text/plain", strings.NewReader(data))

4.

resp, err := http.Post("https://httpbin.org/post", "text/plain", &data)

Q 19 / 40

Go

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

1.

Saveable

2.

SaveInterface

3.

ISave

4.

Saver

Q 20 / 40

Go

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

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._

1.

does not create; creates

2.

does not create; does not create

3.

creates; creates

4.

creates; does not create

Q 21 / 40

Go

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)._

1.

The default behavior is case insensitive, but it can be overridden.

2.

Fields are matched case sensitive.

3.

Fields are matched case insensitive.

4.

The default behavior is case sensitive, but it can be overridden.

Q 22 / 40

Go

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

1.

Time.Add() is for performing addition while Time.Sub() is for nesting timestamps.

2.

Time.Add() always returns a later time while time.Sub always returns an earlier time.

3.

They are opposites. Time.Add(x) is the equivalent of Time.Sub(-x).

4.

Time.Add() accepts a Duration parameter and returns a Time while Time.Sub() accepts a Time parameter and returns a Duration.

Q 23 / 40

Go

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

1.

Every field must have all tags to compile.

2.

It tightly couples different layers of your application.

3.

Any tags after the first are ignored.

4.

Missing tags panic at runtime.

Q 24 / 40

Go

Where is the built-in recover method useful?

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._

1.

in the main function

2.

immediately after a line that might panic

3.

inside a deferred function

4.

at the beginning of a function that might panic

Q 25 / 40

Go

Which choice does _not_ send output to standard error?

1.

println(message)

2.

log.New(os.Stderr, "", 0).Println(message)

3.

fmt.Errorf("%sn", message)

4.

fmt.Fprintln(os.Stderr, message)

Q 26 / 40

Go

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

1.

Use a proxy.

2.

Change the import path.

3.

Use a replace directive in go.mod.

4.

Use a replace directory.

Q 27 / 40

Go

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

Relevant excerpt from the article: > _Relative patterns are also allowed, like "go test ./..." to test all subdirectories._

1.

go test all

2.

go run --all

3.

go test .

4.

go test ./...

Q 28 / 40

Go

Which encodings can you put in a string?

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._

1.

any, it accepts arbitary bytes

2.

any Unicode format

3.

UTF-8 or ASCII

4.

UTF-8

Q 29 / 40

Go

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

Explanation: 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.

1.

There is no difference.

2.

t.Fatal does not crash the test harness, preserving output messages.

3.

t.Fatal stops execution of the subtest and continues with other test cases.

4.

t.Fatal stops all tests and contains extra information about the failed subtest.

Q 30 / 40

Go

What does `log.Fatal` do?

Relevant excerpt from the article: > _Fatal is equivalent to Print() followed by a call to os.Exit(1)._

1.

It raises a panic.

2.

It prints the log and then raises a panic.

3.

It prints the log and then safely exits the program.

4.

It exits the program.

Q 31 / 40

Go

Which is a valid Go time format literal?

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._

1.

"2006-01-02"

2.

"YYYY-mm-dd"

3.

"y-mo-d"

4.

"year-month-day"

Q 32 / 40

Go

How should you log an error (err)

Explanation: There is defined neither log.ERROR, nor log.Error() in [log package](https://pkg.go.dev/log); log.Print() arguments are handled in the manner of fmt.Print(); log.Printf() arguments are handled in the manner of fmt.Printf().

1.

`log.Error(err)`

2.

`log.Printf("error: %v", err)`

3.

`log.Printf(log.ERROR, err)`

4.

`log.Print("error: %v", err)`

Q 33 / 40

Go

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

1.

any other call to lock that Mutex

2.

all goroutines

3.

any writes to the variable it is locking

4.

any reads or writes of the variable is it locking

Q 34 / 40

Go

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

1.

any that starts with `test`

2.

any files that include the word `test`

3.

only files in the root directory that end in `_test.go`

4.

any that ends in `_test.go`

Q 35 / 40

Go

What will be the output of this code?

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

1.

0

2.

It will deadlock

3.

It will not compile

4.

2.718

Q 36 / 40

Go

What will be the output of this program?

ch := make(chan int) close(ch) val := <-ch fmt.Println(val)

1.

It will deadlock

2.

It will panic

3.

0

4.

NaN

Q 37 / 40

Go

What will be printed in this code?

var stocks map[string]float64 // stock -> price price := stocks["MSFT"] fmt.Println("%fn", price)

1.

0

2.

0.000000

3.

The code will panic

4.

NaN

Q 38 / 40

Go

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

1. [stackoverflow](https://stackoverflow.com/questions/9985559/organizing-a-multiple-file-go-project/51942843#51942843) 2. [medium](https://medium.com/@benbjohnson/structuring-applications-in-go-3b04be4ff091) 3. [medium](https://medium.com/golang-learn/go-project-layout-e5213cdcfaa2)

1.

Have a cmd directory and a directory per executable inside it.

2.

Comment out main.

3.

Use build tags.

4.

Have a pkg directory and a directory per executable inside it.

Q 39 / 40

Go

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

1.

Set GOOS to **arm64** and GOARCH to **darwin**.

2.

Set GOOS to **osx** and GOARCH to **arm64**.

3.

Set GOOS to **arm64** and GOARCH to **osx**.

4.

Set GOOS to **darwin** and GOARCH to **arm64**.

Q 40 / 40