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. 😀
main main
"Hello, world?
?fmt.Printf("Hello, world")
os.Args
. (You have to import os
.)
s
on separator sep
? How do you join the elements of s
with separator sep
?strings.Split(s, sep) strings.Join(s, sep)
[]int
?if
statement.
x, y = y, x
found
with initial value false
.var found bool var found bool = false var found = false found := false
for condition { body }
a
?for i := range a { body }
a
?for _, x := range a { body }
int8(byte) int16 int32(rune) int64 uint8 uint16 uint32 uint64 int uint uintptr
float32 float64 complex64 complex128
int32
, complex128
, bool
, string
.0 0+0i false ""
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).
[3]bool
, []string
, struct {X int; Y string}
, map[string][float64]
?[false false false] [] {0 ""} []
func(int)int
, \*complex128
, interface{}
, chan bool
?nil nil nil nil
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 }
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 }
"Dog"
and "Rat"
mapped to true
but "Cat"
mapped to false
.map[string]bool{"Dog": true, "Rat": true, "Cat": false}
x
in map p
to be 21?p
, one pair per line?for k, v := range p { fmt.Println(k, v) }
return (x, y)
in Go? Why or why not?(, 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.
new
operator to allocate memory dynamically as does C++ and Java. What do you do instead to allocate memory? Why does this work?&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.
"thelma"
and "louise"
, in that order?[]string{"thelma", "louise"}
a
? How do you get its capacity?len(a) cap(a)
make([]bool, 5)
and make([]bool, 5, 8)
do?make([]int, 5)
display when printed with fmt.Println
?[0 0 0 0 0]
var a [10]int; b := a[5:7]; c := a[2:6];
, what are the lengths and capacities of b
and c
?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...)
=
.
a
, then pass a[:]
to the function.
TripleJumper
for objects that can hop, skip, and jump.type TripleJumper interface { Hop() Skip() Jump() }
3
, "dog"
, and true
.[]interface{}{3, "dog", true}