当前位置:网站首页>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 .
边栏推荐
- Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
- 【网络教程】IPtables官方教程--学习笔记2
- Struct, enum type and union
- Wokerman custom write log file
- Huatai Securities account opening process, is it safe to open an account on your mobile phone
- 文件操作详解
- Product principles of non-financial decentralized application
- Behind every piece of information you collect, you can't live without TA
- Kubernetes进阶部分学习笔记
- How to choose a microservice registration center?
猜你喜欢

Brush questions with binary tree (4)

Add startup software items when the win system starts up

How to obtain the subordinate / annotation information of KEGG channel

Matlab---eeglab check EEG signal

Detailed explanation of document operation

数据库清空表数据并让主键从1开始

Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34

Struct, enum type and union

Jmeter分布式压测

leetcode-6131:不可能得到的最短骰子序列
随机推荐
Huatai Securities account opening process, is it safe to open an account on your mobile phone
preprocessor directives
7.23
process.env
Niuke-top101-bm38
Vulnhub | dc: 5 | [actual combat]
Yolov7 training error indexerror: list index out of range
Canvas fill gradient
一道golang中关于recover的面试题
Differences between seaslog and monolog log systems, installation steps of seaslog [easy to understand]
黑盒(功能)测试基本方法
seven point two three
Use of C log4net: add file name and line number to the output log content; Repackaged class output file name and line number
一道golang中关于接口和实现的面试题
Canvas 填充渐变
LeetCode通关:哈希表六连,这个还真有点简单
[cloud native] use of Nacos taskmanager task management
C language file reading and writing
[advanced drawing of single cell] 07. Display of KEGG enrichment results
day04_ array