当前位置:网站首页>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 .
边栏推荐
- Introduction to MySQL engine and InnoDB logical storage structure
- Golang language quickly get started to comprehensive practical notes (go language, beego framework, high concurrency chat room, crawler)
- Success factors of software R & D effectiveness measurement
- Scan delete folder problems
- 一道golang中defer和函数结合的面试题
- Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
- Explain in detail the principle of MySQL master-slave replication "suggestions collection"
- Remote - actual combat
- Leetcode customs clearance: hash table six, this is really a little simple
- Product principles of non-financial decentralized application
猜你喜欢

Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!

leetcode-155:最小栈
![[tensorrt] dynamic batch reasoning](/img/59/42ed0074de7162887bfe2c81891e20.png)
[tensorrt] dynamic batch reasoning

Leetcode-6130: designing digital container systems
![[MCU] 51 MCU burning those things](/img/fa/8f11ef64a18114365c084fff7d39f6.png)
[MCU] 51 MCU burning those things

两数,三数之和

Character function and string function (2)
![[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

Matlab---eeglab check EEG signal
![Vulnhub | dc: 6 | [actual combat]](/img/7e/de7d5b56724bde5db2bb8338c35aa8.png)
Vulnhub | dc: 6 | [actual combat]
随机推荐
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
Yolov7 training error indexerror: list index out of range
Leetcode-6127: number of high-quality pairs
Implementation of simple registration and login
Use Navicat to connect to MySQL database through SSH channel (pro test is feasible)
leetcode-114:二叉树展开为链表
【TensorRT】动态batch进行推理
Matlab---eeglab check EEG signal
Leetcode-146: LRU cache
Brush questions with binary tree (4)
process.env
Too many passwords, don't know how to record? Why don't you write a password box applet yourself
[cloud native] use of Nacos taskmanager task management
Leetcode skimming -- guess the size of numbers II 375 medium
作为测试,如何理解线程同步异步
一道golang中defer和函数结合的面试题
MySQL master-slave replication data synchronization, summary of common problems
leetcode-6131:不可能得到的最短骰子序列
Introduction to several scenarios involving programming operation of Excel in SAP implementation project
LeetCode通关:哈希表六连,这个还真有点简单