1. 在A和B处填入代码,使输出为foo
| package main
type S struct
{
m string
}
func f()S {
return __ //A
_}
*func main() {
p:=__ //B
_print(p.m)
} |
| —- |
package main
type S struct
{
m string
}
func f()*S {
return _&S{"foo"} //A
}
func main() {
p:=_f()__ //B
print(p.m)
}
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)
}
} |
| —- |
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)
}
}
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))
} |
| —- |
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))
}
4. 请描述golang语言的初始化顺序:包,变量,常量,init函数
main包->import包->常量->变量->init函数
5. 描述下面代码输出
package main
import "fmt"
type S1 struct {
}
func (s1 S1) f() {
fmt.Println("S1.f()")
}
func (s1 S1) g() {
fmt.Println("S1.g()")
}
type S2 struct
{
S1
}
func (s2 S2) f() {
fmt.Println("S2.f()")
}
type I interface
{
f()
}
func printType(i I) {
if s1,
ok := i.(S1); ok {
s1.f()
s1.g()
}
if s2,
ok := i.(S2); ok {
s2.f()
s2.g()
}
}
func main() {
printType(S1{})
printType(S2{})
}
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))
} |
| —- |
package main
import (
"sync"
"math/rand"
)
const N = 10
func main() {
m := make(map[int]int)
wg := &sync.WaitGroup{}
wg.Add(N)
mu:=sync.Mutex{}
for i :=0; i < N;i++ {
go func() {
defer wg.Done()
mu.Lock()
m[rand.Int()] = rand.Int()
mu.Unlock()
}()
}
wg.Wait()
println(len(m))
}
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)
} |
| —- |
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)
}
9. 下面代码问题是什么,如何修改
| package main
type S struct
{
name string
}
func main() {_
_m := map[string]S{“x”: S{“one”}}
m[“x”].name = “two”
} |
| —- |
package main
type S struct
{
name string
}
func main() {
m := map[string]S{"x": &S{"one"}}
m["x"].name = "two"
}
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)
} |
| —- |
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)
}
11. 描述golang中的stack和heap的区别,分别在什么情况下会分配stack?又在何时会分配到heap中
区别:
栈:一般存放变量名,比较小,系统自动处理内存分配和释放
堆:一般存放具体数据,比较大,一般程序员处理内存
例如 a:=123 a在栈,123在堆