Go Quizzes 101

You think you have mastered Go, right? Do the following quizzes to verify it! sort by date/random group by category series * compile okay or not * comparison rules * swtich duplicate expr, ... * comparable or not * panic or not series * print what options: * result 1 * result 2 * the program will panic * the program doesn't compile * the program is poorly written content * { on the next of switch * type assertion with bad type panic * assume type for bit shift ====================== behavior prediction 1. doesn't compile 2. panic at run time 3. print xxx ===================== add one line to make it work ====================== compile or not https://github.com/golang/go/issues/21738 https://github.com/golang/go/issues/29469 package main func main() { //var _ = ([6]int{})[:] // slice of unaddressable value var _ = (&[6]int{})[:] // ok var _ = (*&[6]int{})[:] // ok } package main func main() { var a interface{} switch a { case int64(123): case int32(123): } var a int switch a { case 123: case 123: } } package main func main() { var a interface{} switch a { case interface{}(123): case 123: // ok for gc case interface{}(nil): case nil: // not ok for gc } } // gccgo ok case 5, 6, 7, 8: { fallthrough } A{}.AStartMethod() new(A).AMethod() var m = map[interface{}]int var s = []int{1, 2, 3} m[s] = 1 // compiles ok but panic var a interace{} _ = a == s const _ = "hello"[0] // no ====================== panic or not? var a interace{} var s = []int{1, 2, 3} var b interace{} = s _ = a == b _ = b == b var v interface{} var _ interface{} = v.(interface{}) // will panic type S struct { x int } func (S) M() {} func main() { type T struct { *S } var t = T{&S{}} println(t.M) // ok println(t.x) // panic } ====================== print what? 1 panic examples 2 switch false { case false: case true: } 3. multiple assignment https://play.golang.org/p/fsMc91mDj1R 4. -128/-1 = -128 5. package main import "reflect" import "fmt" func main() { var v reflect.Value fmt.Println(v.String()) // fmt.Println(v) // ? // } 6. interface{}(nil) == (*int)(nil) // false interface{}(nil) == []int(nil) // doesn't comile nil == []int(nil) // true 7. type loop *loop x := new(loop); *x = x y := new(loop); *y = y fmt.Println(reflect.DeepEqual(x, y)) // true func F() {} var x, y = F, F fmt.Println(reflect.DeepEqual(x, y)) // false 8. (1.13+) var a =====================