1. 在A和B处填入代码,使输出为foo

| package main

type S struct {
m string
}

func f()S {
return __ //A
_}

*func
main() {
p:=__ //B
_print(p.m)
} | | —- |

  1. package main
  2. type S struct
  3. {
  4. m string
  5. }
  6. func f()*S {
  7. return _&S{"foo"} //A
  8. }
  9. func main() {
  10. p:=_f()__ //B
  11. print(p.m)
  12. }

2. 下面代码输出是什么,若想输出012,怎么改

| package main

const N = 3

func main() {
m := make(map[int]int)
for i := 0; i < N; i++ {
m[i] = &i
}
for _, v := range m {
print(
v)
}
} | | —- |

  1. package main
  2. const N = 3
  3. func main() {
  4. m := make(map[int]int)
  5. for i :=0; i < N;i++ {
  6. m[i] = i
  7. }
  8. for _, v:= range m {
  9. print(v)
  10. }
  11. }

3. 代码输出什么?为什么?如何改会使得len(m)为10

| package main

import “sync”

const N
= 10

func main() {
m := make(map[int]int)
wg := &sync.WaitGroup{}
mu := &sync.Mutex{}
wg.Add(N)
for i := 0; i < N; i++ {
go func() {
defer wg.Done()
mu.Lock()
m[i] = i
mu.Unlock()
}()
}
wg.Wait()
println(len(m))
} | | —- |

  1. package main
  2. import "sync"
  3. const N = 10
  4. func main() {
  5. m := make(map[int]int)
  6. wg := &sync.WaitGroup{}
  7. mu := &sync.Mutex{}
  8. wg.Add(N)
  9. for i :=
  10. 0; i < N;
  11. i++ {
  12. go func() {
  13. defer wg.Done()
  14. mu.Lock()
  15. m[i] = i
  16. mu.Unlock()
  17. }()
  18. }
  19. wg.Wait()
  20. println(len(m))
  21. }

4. 请描述golang语言的初始化顺序:包,变量,常量,init函数

main包->import包->常量->变量->init函数

5. 描述下面代码输出

  1. package main
  2. import "fmt"
  3. type S1 struct {
  4. }
  5. func (s1 S1) f() {
  6. fmt.Println("S1.f()")
  7. }
  8. func (s1 S1) g() {
  9. fmt.Println("S1.g()")
  10. }
  11. type S2 struct
  12. {
  13. S1
  14. }
  15. func (s2 S2) f() {
  16. fmt.Println("S2.f()")
  17. }
  18. type I interface
  19. {
  20. f()
  21. }
  22. func printType(i I) {
  23. if s1,
  24. ok := i.(S1); ok {
  25. s1.f()
  26. s1.g()
  27. }
  28. if s2,
  29. ok := i.(S2); ok {
  30. s2.f()
  31. s2.g()
  32. }
  33. }
  34. func main() {
  35. printType(S1{})
  36. printType(S2{})
  37. }

6. 下面代码有什么问题,怎么修改

| package main

import (
“sync”
“math/rand”
)

const N = 10

func main() {
m := make(map[int]int)
wg := &sync.WaitGroup{}
wg.Add(N)
for i := 0; i < N; i++ {
go func() { defer wg.Done()
m[rand.Int()] = rand.Int() }()
}
wg.Wait()
println(len(m))
} | | —- |

  1. package main
  2. import (
  3. "sync"
  4. "math/rand"
  5. )
  6. const N = 10
  7. func main() {
  8. m := make(map[int]int)
  9. wg := &sync.WaitGroup{}
  10. wg.Add(N)
  11. mu:=sync.Mutex{}
  12. for i :=0; i < N;i++ {
  13. go func() {
  14. defer wg.Done()
  15. mu.Lock()
  16. m[rand.Int()] = rand.Int()
  17. mu.Unlock()
  18. }()
  19. }
  20. wg.Wait()
  21. println(len(m))
  22. }

7. 请描述make和new的区别

make 被用来分配引用类型的内存: map, slice, channel
new 被用来分配除了引用类型的所有其他类型的内存: int, string, array等

8. 下面代码输出什么,如何让输出为true

| package main

import “fmt”

type
S struct {
a, b, c string
}

func main() {
x := interface{}(&S{“a”, “b”, “c”})
y := interface{}(&S{“a”, “b”, “c”})
fmt.Println(x == y)
} | | —- |

  1. package main
  2. import "fmt"
  3. type S struct {
  4. a, b, c string
  5. }
  6. func main() {
  7. x := interface{}(S{"a", "b", "c"})
  8. y := interface{}(S{"a", "b", "c"})
  9. fmt.Println(x == y)
  10. }

9. 下面代码问题是什么,如何修改

| package main

type S struct {
name string
}

func main() {_ _m := map[string]S{“x”: S{“one”}}
m[“x”].name = “two”
} | | —- |

  1. package main
  2. type S struct
  3. {
  4. name string
  5. }
  6. func main() {
  7. m := map[string]S{"x": &S{"one"}}
  8. m["x"].name = "two"
  9. }

10. 修改代码,使得status输出为200

| package main

import (
“encoding/json”
“fmt”
)

type Result struct {
status int }

func main() {
var data = []byte({"status":200})
result := &Result{}
if err := json.Unmarshal(data, &result); err != nil {
fmt.Println(“error:”, err)
return
}
fmt.Printf(“result=%+v”, result)
} | | —- |

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Result struct {
  7. Status int
  8. }
  9. func main() {
  10. var data= []byte(`{"status":200}`)
  11. result := &Result{}
  12. if err:= json.Unmarshal(data, &result); err != nil {
  13. fmt.Println("error:", err)
  14. return
  15. }
  16. fmt.Printf("result=%+v", result)
  17. }

11. 描述golang中的stack和heap的区别,分别在什么情况下会分配stack?又在何时会分配到heap中

区别:
栈:一般存放变量名,比较小,系统自动处理内存分配和释放
堆:一般存放具体数据,比较大,一般程序员处理内存
例如 a:=123 a在栈,123在堆