Go
General 13 questions
- #1What are some characteristics of the Go programming language?
- #2What is the difference between var x int = 2 and x := 2?
- #3True or False? In Go we can redeclare variables and once declared we must use it.
- #4What libraries of Go have you used?
- #5What is the problem with the following block of code? How to fix it?
func main() { var x float32 = 13.5 var y int y = x } - #6The following block of code tries to convert the integer 101 to a string but instead we get "e". Why is that? How to fix it?
package main import "fmt" func main() { var x int = 101 var y string y = string(x) fmt.Println(y) } - #7What is wrong with the following code?:
package main func main() { var x = 2 var y = 3 const someConst = x + y } - #8What will be the output of the following block of code?:
package main import "fmt" const ( x = iota y = iota ) const z = iota func main() { fmt.Printf("%v\n", x) fmt.Printf("%v\n", y) fmt.Printf("%v\n", z) } - #9What _ is used for in Go?
- #10What will be the output of the following block of code?:
package main import "fmt" const ( _ = iota + 3 x ) func main() { fmt.Printf("%v\n", x) } - #11What will be the output of the following block of code?:
package main import ( "fmt" "sync" "time" ) func main() { var wg sync.WaitGroup wg.Add(1) go func() { time.Sleep(time.Second * 2) fmt.Println("1") wg.Done() }() go func() { fmt.Println("2") }() wg.Wait() fmt.Println("3") } - #12What will be the output of the following block of code?:
package main import ( "fmt" ) func mod1(a []int) { for i := range a { a[i] = 5 } fmt.Println("1:", a) } func mod2(a []int) { a = append(a, 125) // ! for i := range a { a[i] = 5 } fmt.Println("2:", a) } func main() { s1 := []int{1, 2, 3, 4} mod1(s1) fmt.Println("1:", s1) s2 := []int{1, 2, 3, 4} mod2(s2) fmt.Println("2:", s2) } - #13What will be the output of the following block of code?:
package main import ( "container/heap" "fmt" ) // An IntHeap is a min-heap of ints. type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *IntHeap) Push(x interface{}) { // Push and Pop use pointer receivers because they modify the slice's length, // not just its contents. *h = append(*h, x.(int)) } func (h *IntHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func main() { h := &IntHeap{4, 8, 3, 6} heap.Init(h) heap.Push(h, 7) fmt.Println((*h)[0]) }