Go Reinforcement Practice

Here are a set of problems designed to help you reinforce and retain some useful JavaScript knowledge. If you are an Anki or Quizlet fan, consider adding some of these questions into a deck. 😀


Who created Go and in what year? Google, 2009.
What kind of animal is the mascot for Go? A gopher
What kinds of problems was Go crated to solve? Large scale “Google-sized” problems, running on distributed systems that must be efficient and reliable.
Go programs begin by calling a function called ________________ inside a package called ________________.
main
main
What line of code writes "Hello, world??
fmt.Printf("Hello, world")
Must every Go file have a package declaration or is it optional? It is required. There is no such thing as a default package.
How do you get the command line arguments in a Go program? They are in os.Args. (You have to import os.)
How do you split a string s on separator sep? How do you join the elements of s with separator sep?
strings.Split(s, sep)
strings.Join(s, sep)
How do you pronounce the type []int? Slice of integers.
What does the conditional expression look like in Go? Go does not have a conditional expression. You have to use an if statement.
How do you swap the values of two variables?
x, y = y, x
Show four different syntaxes for declaring a local variable found with initial value false.
var found bool
var found bool = false
var found = false
found := false
Can a variable ever be underfined in Go? No, if a variable is not explictly initialized in code, Go will initialize it with the zero-value of its type.
How do you write a while-loop in Go?
for condition { body }
How do you iterate through the indices of array a?
for i := range a { body }
How do you iterate through the values of array a?
for _, x := range a { body }
Name all of the built-in integer types in Go. Include all aliases.
int8(byte)   int16   int32(rune)   int64
uint8   uint16   uint32   uint64
int   uint   uintptr
Name all of the built-in floating point and complex number types in Go.
float32   float64
complex64   complex128
For each of the following types, give their default values: int32, complex128, bool, string.
0
0+0i
false
""
What is the value of len("こんにちは世界")? Why is it not 7? What expression, using len and the string "こんにちは世界", does give 7? len("こんにちは世界") is 21 because the UTF-8 encoding of the string has 21 bytes (each rune happens to be encoded in three bytes). The expression len([]rune("こんにちは世界")) is 7, because casting a string to a rune slice will give you a slice with each rune (code point).
Name the 8 composite types of Go. Arrays, functions, structs, maps, pointers, slices, interfaces, channels.
What are the default values of the types [3]bool, []string, struct {X int; Y string}, map[string][float64]?
[false false false]
[]
{0 ""}
[]
What are the default values of the types func(int)int, \*complex128, interface{}, chan bool?
nil
nil
nil
nil
Give a Go function called DivRem which accepts two integers, x and y, and returns their integer quotient and integer remainder, respectively. Return the values in a single return statement.
func DivRem(x, y int) (int, int) {
    return x / y, x % y
}
Give a Go function called DivRem which accepts two integers, x and y, and returns their integer quotient and integer remainder, respectively. Use named return values.
func DivRem(x, y int) (quotent int, remainder int) {
    quotient = x / y
    remainder = x % y
    return
}
Give an expression for a map in which "Dog" and "Rat" mapped to true but "Cat" mapped to false.
map[string]bool{"Dog": true, "Rat": true, "Cat": false}
How do you add or update the corresponding value at key x in map p to be 21? p["x"] = 21
How do you print each of the key-value pairs of map p, one pair per line?
for k, v := range p {
    fmt.Println(k, v)
}
Can we ever write an expression like return (x, y) in Go? Why or why not? No. Go does not have tuples (nor a comma operator), so the expression (, y) is a syntax error. You can however write return x, y. But note return multiple values is absolutely not the same as returning a tuple.
Go doesn’t have a new operator to allocate memory dynamically as does C++ and Java. What do you do instead to allocate memory? Why does this work? You simply write a pointer to an expression, e.g., &Tree{value, nil}. Although this seems to be creating a pointer to a temporary value in the current stack frame, Go will escape it to the heap if necessary.
How do you write the slice containing the values "thelma" and "louise", in that order?
[]string{"thelma", "louise"}
How do you get the length of a slice a? How do you get its capacity?
len(a)
cap(a)
What do the expressions make([]bool, 5) and make([]bool, 5, 8) do? The first makes a slice whose length and capacity are both 5. The second makes a slice with length 5 and capacity 8.
How does the value make([]int, 5) display when printed with fmt.Println?
[0 0 0 0 0]
Given var a [10]int; b := a[5:7]; c := a[2:6];, what are the lengths and capacities of b and c? Length of b = 2
Capacity of b = 5
Length of c = 4
Capacity of c = 8
Given integer slices a and b, how do you append the values 5 and 8 to slice s? How do you append all of slice t to slice s?
s = append(s, 5, 8)
s = append(s, t...)
How do you copy one array into another? Just assign it with =.
If a function is declared to take in a slice parameter, can you pass it an array? Why or why not? If not, how can you get a function operating on a slice to work on your array? No, arrays are not slices. They are not compatible, nor will Go ever implcitly covert an array to a slice. If you have an array a, then pass a[:] to the function.
What is the most significant way in which Go interfaces differ from those in Java? In Java, a class must explicitly state that it is implementing an interface; in Go, a struct implements in interface simply by defining the appropriate methods.
Define an interface called TripleJumper for objects that can hop, skip, and jump.
type TripleJumper interface {
    Hop()
    Skip()
    Jump()
}
Give an expression for a slice containing the values 3, "dog", and true.
[]interface{}{3, "dog", true}