context
package main

import (
	"context"
	"fmt"
)

func f(ctx context.Context) {
	context.WithValue(ctx, "foo", -6)
}

func main() {
	ctx := context.TODO()
	f(ctx)
	fmt.Println(ctx.Value("foo"))
}
  • A. -6
  • B. 0
  • C. <nil>
  • D: panic
Answer
Try it
C
context1
package main

import(
	"fmt"
	"encoding/json"
	"context"
)

func main() {
	data, _ := json.Marshal(context.WithValue(context.Background(), "a", "b"))
	fmt.Println(string(data))
}
Answer
Try it
{"Context":{}}
defer
package main

import (
    "fmt"
)

func main() {
    defer_call()
}

func defer_call() {
    defer func() { fmt.Println("1") }()
    defer func() { fmt.Println("2") }()
    defer func() { fmt.Println("3") }()

    panic("4")
}
Answer
Try it
3
2
1
panic: 4
defer1
package main

import (
	"fmt"
)

type Person struct {
	age int
}

func main() {
	person := &Person{28}

	defer fmt.Println(person.age)

	defer func(p *Person) {
		fmt.Println(p.age)
	}(person)

	defer func() {
		fmt.Println(person.age)
	}()

	person.age = 29
}
Answer
Try it
29
29
28
defer2
package main

import (
	"fmt"
)

type Person struct {
	age int
}

func main() {
	person := &Person{28}

	defer fmt.Println(person.age)

	defer func(p *Person) {
		fmt.Println(p.age)
	}(person)

	defer func() {
		fmt.Println(person.age)
	}()

	person = &Person{29}
}
Answer
Try it
29
28
28
defer3
package main

import (
	"fmt"
)

var a bool = true

func main() {
	defer func() {
		fmt.Println("1")
	}()
	if a == true {
		fmt.Println("2")
		return
	}
	defer func() {
		fmt.Println("3")
	}()
}
Answer
Try it
2
1
defer4
package main

import "fmt"

type temp struct{}

func (t *temp) Add(elem int) *temp {
	fmt.Print(elem)
	return &temp{}
}

func main() {
	tt := &temp{}
	defer tt.Add(1).Add(2)
	tt.Add(3)
}
  • A. 132
  • B. 123
  • C. 312
  • D. 321
Answer
Try it
A
goroutine
package main

import (
	"fmt"
	"time"
)

func main() {
	ch1 := make(chan int)
	go fmt.Println(<-ch1)
	ch1 <- 5
	time.Sleep(1 * time.Second)
}
  • A. 5
  • B. deadlock
  • C. compilation error
Answer
Try it
B
interface
package main

import (
	"fmt"
)

type People interface {
	Show()
}

type Student struct{}

func (stu *Student) Show() {

}

func live() People {
	var stu *Student
	return stu
}

func main() {
	if live() == nil {
		fmt.Println("AAAAAAA")
	} else {
		fmt.Println("BBBBBBB")
	}
}
Answer
Try it
BBBBBBB
interface1
package main

import (
	"fmt"
)

type People interface {
	Speak(string) string
}

type Student struct{}

func (stu *Student) Speak(think string) (talk string) {
	if think == "love" {
		talk = "You are a good boy"
	} else {
		talk = "hi"
	}
	return
}

func main() {
	var peo People = Student{}
	think := "love"
	fmt.Println(peo.Speak(think))
}
Answer
Try it
compilation error
cannot use Student{} (value of type Student) as People value in variable declaration: Student does not implement People (method Speak has pointer receiver)
interface2
package main

import "fmt"

type T1 struct {
	String func() string
}

func (T1) Error() string {
	return "T1.Error"
}

type T2 struct {
	Error func() string
}

func (T2) String() string {
	return "T2.String"
}

var t1 = T1{String: func() string { return "T1.String" }}
var t2 = T2{Error: func() string { return "T2.Error" }}

func main() {
	fmt.Println(t1.Error(), t1.String())
	fmt.Println(t2.Error(), t2.String())

	fmt.Println(t1, t2)
}
Answer
Try it
T1.Error T1.String
T2.Error T2.String
T1.Error T2.String
interface3
package main

import "fmt"

func main() {
	var p [100]int
	var m interface{} = [...]int{99: 0}
	fmt.Println(p == m)
}
Answer
Try it
true
iota
package main

import "fmt"

const (
	x = iota
	_
	y
	z = "zz"
	k
	p = iota
)

func main() {
	fmt.Println(x, y, z, k, p)
}
Answer
Try it
0 2 zz zz 5
iota1
package main

import "fmt"

const (
	a = iota
	b = iota
)

const (
	name = "name"
	c    = iota
	d    = iota
)

func main() {
	fmt.Println(a, b, c, d)
}
Answer
Try it
0 1 1 2
json
package main

import (
	"encoding/json"
	"fmt"
)

type AutoGenerated struct {
	Age   int    `json:"age"`
	Name  string `json:"name"`
	Child []int  `json:"child"`
}

func main() {
	jsonStr1 := `{"age": 14,"name": "potter", "child":[1,2,3]}`
	a := AutoGenerated{}
	json.Unmarshal([]byte(jsonStr1), &a)
	aa := a.Child
	fmt.Println(aa)
	jsonStr2 := `{"age": 12,"name": "potter", "child":[3,4,5,7,8,9]}`
	json.Unmarshal([]byte(jsonStr2), &a)
	fmt.Println(aa)
}
  • A. [1 2 3] [1 2 3]
  • B. [1 2 3] [3 4 5]
  • C. [1 2 3] [3 4 5 6 7 8 9]
  • D. [1 2 3] [3 4 5 0 0 0]
Answer
Try it
B
json1
package main

import (
	"encoding/json"
	"fmt"
	"time"
)

func main() {
	t := struct {
		time.Time
		N int
	}{
		time.Date(2020, 12, 20, 0, 0, 0, 0, time.UTC),
		5,
	}

	m, _ := json.Marshal(t)
	fmt.Printf("%s", m)
}
  • A. {“Time”:“2020-12-20T00:00:00Z”,“N”:5}
  • B. “2020-12-20T00:00:00Z”
  • C. {“N”:5}
  • D. <nil>
  • E. 其他
Answer
Try it
B
len
package main

func main() {
	var x *struct {
		s [][32]byte
	}

	println(len(x.s[99]))
}
  • A. panic
  • B. compilation error
  • C. 32
  • D. 0
Answer
Try it
C
len1
package main

const s = "Go101.org"

// len(s) == 9
// 1 << 9 == 512
// 512 / 128 == 4

var a byte = 1 << len(s) / 128
var b byte = 1 << len(s[:]) / 128

func main() {
	println(a, b)
}
  • A: 0 0
  • B: 0 4
  • C: 4 0
  • D: 4 4
Answer
Try it
C
len2
package main

var s = "Go101.org"

// len(s) == 9
// 1 << 9 == 512
// 512 / 128 == 4

var a byte = 1 << len(s) / 128
var b byte = 1 << len(s[:]) / 128

func main() {
	println(a, b)
}
  • A: 0 0
  • B: 0 4
  • C: 4 0
  • D: 4 4
Answer
Try it
A
map
package main

import "fmt"

type Student struct {
  Name string
}

var list map[string]Student

func main() {

	list = make(map[string]Student)

	student := Student{"Aceld"}

	list["student"] = student
	list["student"].Name = "LDB"

	fmt.Println(list["student"])
}
Answer
Try it
compilation error
cannot assign to struct field list["student"].Name in map
map1
package main

func main() {
	m := make(map[int]int, 3)
	x := len(m)
	m[1] = m[1]
	y := len(m)
	println(x, y)
}
Answer
Try it
0 1
print
package main

import "fmt"

type T struct {
	x int
}

func (t T) String() string { return "boo" }

func main() {
	t := T{123}
	fmt.Printf("%v\n", t)
	fmt.Printf("%#v\n", t)
}
Answer
Try it
boo
main.T{x:123}
print1
package main

import (
	"fmt"
)

func f(a ...int) {
	fmt.Printf("%#v\n", a)
}

func main() {
	f()
}
Answer
Try it
[]int(nil)
race
type Stats struct {
    mutex sync.Mutex

    counters map[string]int
}

func (s *Stats) Snapshot() map[string]int {
    s.mutex.Lock()
    defer s.mutex.Unlock()

    return s.counters
}

func (s *Stats) Add(name string, num int) {
    s.mutex.Lock()
    defer s.mutex.Unlock()
    s.counters[name] = num
}
Answer
func (s *Stats) Snapshot() map[string]int {
	s.mutex.Lock()
	defer s.mutex.Unlock()
result := make(map[string]int, len(s.counters))
for k, v := range s.counters {
	result[k] = v
}
return result

}