Several Golang face questions and analysis

Source: Internet
Author: User
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

1
2
ShowA
Showb

Analytical

There is no inheritance in go, only combinations. The people in teacher is an anonymous object that is called by its own function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.