This is a creation in Article, where the information may have evolved or changed.
The output of the following code is:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Package main Import ( "FMT" ) func main() { Defer_call () } func defer_call() { defer func() {fmt. Println ("Pre-print")} () defer func() {fmt. Println ("in print")} () defer func() {fmt. Println ("post-print")} () Panic("trigger exception") }
|
Answer
1 2 3 4
|
After printing In print Before printing Panic: Triggering an exception
|
Analytical
In the case of defer and panic combinations, when there is panic, the defer is executed and then the panic is passed.
For more information, see the defer common pits and official document descriptions.
Two, the following code what is wrong
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26
|
Package main Import ( "FMT" ) type student struct { Name string Age int } func pase_student() map[string]*Student { m: = make (map[string]*student) stus: = []student{ {Name: "Zhou", Age: +}, {Name: "Li", Age: +}, {Name: "Wang", Age: +}, } for _, stu: = range Stus { M[stu. Name] = &stu } return m } func main() { Students: = pase_student () for k, V: = Range Students { FMT. Printf ("key=%s,value=%v \ n", K, v) } }
|
Answer
1 2 3
|
Key=wang,value=&{wang 22}
|
Analytical
When using Stu traversal for A For loop, Stu is just a temporary variable, and the pointer address is not changed during traversal, so the subsequent assignment is pointing to the same memory area, resulting in consistent information at the end.
In fact, this phenomenon is not only in the go middle, c/c++ and python also exist, the principle is the same.
modifying scenarios
1 2 3 4
|
for Range Stus { Stu:=stus[i] M[stu. Name] = &stu }
|
Third, what the following code will output and explain why
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24
|
Package main
Import ( "Sync" "FMT" )
func main() { WG: = Sync. waitgroup{} WG. ADD (+) For i: = 0; i < ten; i++ { Go func() { FMT. Println ("I:", i) WG. Done () }() } For J: = 0; j < ; Go func(x int) { FMT. Println ("J:", X) WG. Done () } (j) } WG. Wait () }
|
Answer
1 2 3 4 5 6 7 8 9 10 11 12 13 Span class= ' line ' >14 15 16 17 18 19 20 |
J: 9 I: 10 J: 0 J: 1 J: 2 J: 3 J: 4 J: 5 J: 6 I: 10 I: 10 I: 10 I: 10 I: 10 J: 8 I: 10 I: 10 I: 10 J: 7 I: 10
|
Analytical
The first cycle of printing is printed in the function, I is an external variable, after executing the go func(){} code will not be executed immediately, generally when the code fragment is executed by the scheduler, the for loop has been fully executed, at this time I is 10. So I will print 10 10, and J will print 1-10 out of order.
Iv. what the following code will output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
type people struct{} func (P *people) ShowA() { FMT. Println ("ShowA") P.SHOWB () } func (P *people) showb() { FMT. Println ("SHOWB") } type Teacher struct { people } func (t *teacher) showb() { FMT. Println ("Teacher Showb") } func main() { t: = teacher{} T.showa () }
|
Answer
Analytical
There is no inheritance in go, only combinations. The people in teacher is an anonymous object that is called by its own function.