当前位置:网站首页>An interview question about interface and implementation in golang
An interview question about interface and implementation in golang
2022-07-25 20:54:00 【youngqqcn】
Can the following code be compiled ? Why? ?
package main
import (
"fmt"
)
type People interface {
Speak(string) string
}
type Student struct{
}
func (stu *Student) Speak(think string) (talk string) {
if think == "bitch" {
talk = "You are a good boy"
} else {
talk = "hi"
}
return
}
func main() {
var peo People = Student{
}
think := "bitch"
fmt.Println(peo.Speak(think))
}
,
,
,
,
,
,
,
,
,
,
,
,
,
answer
Compile failed , Value type `Student{}` Interface not implemented `People` Methods , It can't be defined as `People` type .
stay golang In language ,`Student` and `*Student` There are two types , The first is to say `Student` In itself , The second is to point to `Student` The pointer to .
How to rewrite ?
Change method set to value type
type Student struct{
}
// Using value
func (stu Student) Speak(think string) (talk string) {
if think == "bitch" {
talk = "You are a good boy"
} else {
talk = "hi"
}
return
}
func main() {
var peo People = Student{
}
think := "bitch"
fmt.Println(peo.Speak(think))
}
perhaps , Use the pointer
type Student struct{
}
func (stu *Student) Speak(think string) (talk string) {
if think == "bitch" {
talk = "You are a good boy"
} else {
talk = "hi"
}
return
}
func main() {
var peo People = &Student{
} // Use the pointer
think := "bitch"
fmt.Println(peo.Speak(think))
}
Let's look at a similar problem :
Please tell me what the following code will output ? Why? ?
package main
import (
"fmt"
)
type People interface {
Show()
}
type Student struct{
}
func (stu *Student) Show() {
}
func live() People {
var stu *Student
if stu == nil {
fmt.Println("C")
}
return stu
}
func main() {
p := live()
if p == nil {
fmt.Println("A")
} else {
fmt.Println("B")
}
}
Please think about it. , The answer is below
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
answer
C
B
Same as the last question , The difference is *Student There is no initialization value after the definition of , therefore *Student yes nil Of , however *Student Realized People Interface , Interface is not nil.
We compile the code with gdb debug , go build -gcflags "-N -l" solution21.go
$ go build -gcflags "-N -l" solution21.go
$ gdb solution21
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from solution21...
Loading Go Runtime support.
(gdb) l
11 type Student struct{}
12
13 func (stu *Student) Show() {
14
15 }
16
17 func live() People {
18 var stu *Student
19 if stu == nil {
20 fmt.Println("C")
(gdb)
21 }
22 return stu
23 }
24
25 func main() {
26 p := live()
27 if p == nil {
28 fmt.Println("A")
29 } else {
30 fmt.Println("B")
(gdb)
31 }
32 }
(gdb)
Line number 33 out of range; /home/yqq/mine/master-go/interview/6-interview-golang/solution21.go has 32 lines.
(gdb) b 27
Breakpoint 1 at 0x47e147: file /home/yqq/mine/master-go/interview/6-interview-golang/solution21.go, line 27.
(gdb) r
Starting program: /home/yqq/mine/master-go/interview/6-interview-golang/solution21
[New LWP 24400]
[New LWP 24401]
[New LWP 24402]
[New LWP 24403]
[New LWP 24404]
C
Thread 1 "solution21" hit Breakpoint 1, main.main ()
at /home/yqq/mine/master-go/interview/6-interview-golang/solution21.go:27
27 if p == nil {
(gdb) i locals
p = {tab = 0x4b3188 <Student,main.People>, data = 0x0}
(gdb)
You can see , p = {tab = 0x4b3188 <Student,main.People>, data = 0x0}, interface By 2 Part of ,tab and data, When comparing p and nil Of course, time is not equal .
We use it gdb Look at the following code
package main
import "fmt"
func main() {
var p interface{
} = nil
if p == nil {
fmt.Println("A")
} else {
fmt.Println("B")
}
var q interface{
} = (*interface{
})(nil)
if q == nil {
fmt.Println("C")
} else {
fmt.Println("D")
}
}
go build -gcflags "-N -l" solution22.go compile
$ gdb solution22
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from solution22...
Loading Go Runtime support.
(gdb) l
1 package main
2
3 import "fmt"
4
5 func main() {
6 var p interface{
} = nil
7 if p == nil {
8 fmt.Println("A")
9 } else {
10 fmt.Println("B")
(gdb)
11 }
12
13 var q interface{
} = (*interface{
})(nil)
14 if q == nil {
15 fmt.Println("C")
16 } else {
17 fmt.Println("D")
18 }
19
20 }
(gdb) b 7
Breakpoint 1 at 0x47e08c: file /home/yqq/mine/master-go/interview/6-interview-golang/solution22.go, line 7.
(gdb) b 14
Breakpoint 2 at 0x47e109: file /home/yqq/mine/master-go/interview/6-interview-golang/solution22.go, line 14.
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000047e08c in main.main
at /home/yqq/mine/master-go/interview/6-interview-golang/solution22.go:7
2 breakpoint keep y 0x000000000047e109 in main.main
at /home/yqq/mine/master-go/interview/6-interview-golang/solution22.go:14
(gdb) r
Starting program: /home/yqq/mine/master-go/interview/6-interview-golang/solution22
[New LWP 28172]
[New LWP 28173]
[New LWP 28174]
[New LWP 28175]
Thread 1 "solution22" hit Breakpoint 1, main.main ()
at /home/yqq/mine/master-go/interview/6-interview-golang/solution22.go:7
7 if p == nil {
(gdb) p p
$1 = {
_type = 0x0, data = 0x0}
(gdb) c
Continuing.
A
Thread 1 "solution22" hit Breakpoint 2, main.main ()
at /home/yqq/mine/master-go/interview/6-interview-golang/solution22.go:14
14 if q == nil {
(gdb) p q
$2 = {
_type = 0x483360, data = 0x0}
(gdb)
You can see ,p What's printed out is $1 = {_type = 0x0, data = 0x0}
q What's printed out is {_type = 0x483360, data = 0x0}
So to conclude :
golang Medium interface The bottom is made up of 2 Part of , namely (type,data), As long as one of them is not nil, that , When used intefalce And nil The comparison is not equal .
边栏推荐
- "Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
- Cesium 多边形渐变色纹理(Canvas)
- Embedded development: embedded foundation -- threads and tasks
- What's special about Huawei's innovative solutions to consolidate the foundation of ERP for small and medium-sized enterprises?
- [FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes
- Wokerman custom write log file
- Matlab---eeglab check EEG signal
- DDD的Go实战
- Has baozi ever played in the multi merchant system?
- leetcode-79:单词搜索
猜你喜欢

matlab----EEGLab查看脑电信号

leetcode-79:单词搜索

Leetcode-6131: the shortest dice sequence impossible to get

Basic knowledge of Marine Geology

Canvas 填充渐变

作为测试,如何理解线程同步异步

Key network protocols in tcp/ip four layer model
![[FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes](/img/65/4dd3a521946e753c79d3db1fa0a4f4.png)
[FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes

leetcode-6131:不可能得到的最短骰子序列

Leetcode-6127: number of high-quality pairs
随机推荐
Character function and string function (2)
Leetcode-6125: equal row and column pairs
MySQL inserts three tables with different values. The association condition is the primary foreign key. How about the syntax of the insertion statement?
Key network protocols in tcp/ip four layer model
一道golang中关于recover的面试题
leetcode-919:完全二叉树插入器
Qixin Jushi cloud spectrum new chapter | Haitai Fangyuan and Sichuan Unicom reach ecological strategic cooperation
Has baozi ever played in the multi merchant system?
[FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes
Leetcode-6129: number of all 0 subarrays
Rand1 generates rand9
Huatai Securities account opening process, is it safe to open an account on your mobile phone
Pychart automatically enters the test mode when running the program
Vulnhub | dc: 5 | [actual combat]
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input
Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
Niuke-top101-bm37
MPI learning notes (II): two implementation methods of matrix multiplication
Unity vs -- the default debugging in VS is to start rather than attach to unity debugging