当前位置:网站首页>Go learning (IV. interface oriented)
Go learning (IV. interface oriented)
2022-06-29 22:01:00 【Soy sauce elder martial brother】
List of articles
4.1 Interface
go Language supports interfaces , Because the current programming logic is basically based on interfaces .
4.1.1 Interface definition and usage
Let's take a look at the definition of the interface :
// Definition of interface
type Retiever interface {
Get(url string) string // The interface is full of functions
}
The interface definition is relatively simple , Then let's see how it works :
Let's start with main.go:
package main
import (
"fmt"
"./mock"
"./real"
)
type Retiever interface {
Get(url string) string
}
func download(r Retiever) string {
return r.Get("http://www.imooc.com")
}
func main() {
var r Retiever // This defines an interface variable
r = mock.Retiever{
"this is test"} // Then directly assign this object to r
// because go No inheritance, no polymorphism , So as long as the interface contains the same function, it can be directly assigned , It seems to be really convenient
fmt.Println(download(r)) // Function call
r = real.Retiever{
}
fmt.Println(download(r))
}
Now let's see mock.go
package mock
type Retiever struct {
Contents string
}
func (r Retiever)Get(url string) string {
// Is a simple implementation get Method
return r.Contents
}
4.1.2 Summary of the interface
go The interface of is not c++ Inheritance , either java The implementation of the , Is an implicit implementation , As long as the functions in the interface are implemented , You can assign a value to the type of this interface , Although it is written quickly , But will it be chaotic ? We need to see this in the future .
4.1.3 The value type of the interface
Learn from this teacher , Also learn about the values of interface types , This is where we are c++ I never thought about it in my study , This will be done later c++ When , You can analyze .
func main() {
var r Retiever
r = mock.Retiever{
"this is test"}
//fmt.Println(download(r))
fmt.Printf("%T %v\n", r, r)
r = real.Retiever{
}
//fmt.Println(download(r))
fmt.Printf("%T %v\n", r, r)
}
Next, let's look at a wave of printing :
mock.Retiever {
this is test} // There are really types , But they are all value types
real.Retiever {
0s}
Change it to see if you can change it to a pointer type :
func (r *Retiever)Get(url string) string {
return r.Contents
}
Change the receiver here , Then it can be changed to pointer type .
r = &mock.Retiever{
"this is test"}
//fmt.Println(download(r))
fmt.Printf("%T %v\n", r, r)
in addition go The language gets the type of this variable as follows :r.(type)
We can also go through r To get the corresponding value :
real := r.(real.Retiever)
fmt.Println(real.TimeOut)
4.1.4 go Any type of language
go Languages have an arbitrary type ,interface{}.
Can be regarded as go The generics of languages .
type Queue []interface{
} // Define a slice of any type
func (q *Queue) push(Value interface{
}) {
*q = append(*q, Value)
}
var q Queue
q.push(1)
q.push("adv")
fmt.Println(q) // Called function
4.1.5 Interface combination
type Retiever interface {
Get(url string) string
}
type Post interface {
Post(url string, from map[string]string)
}
type all interface {
Retiever
Post
}
This is the combination of interfaces , It's quite simple , And then implement the class , You need to implement both interface functions , Talent .
4.1.6 Common system interfaces
Stringer() adopt fmt.Println Print
Reader()
Writer()
Common system interfaces , It needs to be programmed later , Familiar again .
边栏推荐
- What is unique about MES system?
- Hardware development notes (VIII): basic process of hardware development, making a USB to RS232 module (VII): creating a basic dip component (crystal oscillator) package and associating the principle
- ASP using panel to realize simple registration page
- 【ROS进阶篇】第二讲 自定义头、源文件封装
- After inventing anti-virus software, he chose to be a top-notch gangster
- Golang operation NSQ distributed message queue
- Is it safe to bind securities accounts to flush? Which securities firm can bind flush after opening an account
- Win10添加ssh公钥
- 【ROS进阶篇】第四讲 ROS中的重名问题(节点、话题与参数)
- Graduation summary of construction practice camp
猜你喜欢

As a developer, you need to know about the codeless development platform IVX
![[advanced ROS chapter] lesson 2 custom header and source file encapsulation](/img/25/85e8c55605f5cc999a8e85f0a05f93.jpg)
[advanced ROS chapter] lesson 2 custom header and source file encapsulation

In the shop project, implement a menu (add, delete, modify and query)

Data mining review

Desai wisdom number - other charts (basic sunrise chart): high frequency words in graduation speech

Star ring technology data security management platform defender heavy release

Detailed explanation of MySQL and mvcc and the difference between RC and RR for snapshot reading

yolov6训练自己的数据记录+yolov5对比测试

Golang operation etcd
![[cloud native] use of Nacos taskmanager task management](/img/ad/24bdd4572ef9990238913cb7cd16f8.png)
[cloud native] use of Nacos taskmanager task management
随机推荐
Amazon Keyword Search API interface (item_search- Amazon product search interface by keyword), Amazon API interface
唯品会关键词搜索API接口(item_search-按关键字搜索唯品会商品API接口),唯品会API接口
细说GaussDB(DWS)复杂多样的资源负载管理手段
Hardware development notes (VIII): basic process of hardware development, making a USB to RS232 module (VII): creating a basic dip component (crystal oscillator) package and associating the principle
阿里巴巴商品详情API接口(item_get-获得商品详情接口),阿里巴巴API接口
Detailed explanation of MySQL and mvcc and the difference between RC and RR for snapshot reading
The database of the server cannot be connected [the service has been started, the firewall has been closed, the port has been opened, and the netlent port is not connected]
Bs-gx-017 online examination management system based on SSM
Matlab output format control%d,%f,%c,%s usage
LeetCode 1. Sum of two numbers
[cloud native] use of Nacos taskmanager task management
I want to register my stock account online. How do I do it? In addition, is it safe to open a mobile account?
2022 openvino DevCon unveils secrets! Intel and many partners deepen the construction of developer ecology and release the innovation potential of AI industry
Type of radar
leetcode:307. Area and retrieval - array modifiable
小型圖書館項目總結
亚马逊关键词搜索API接口(item_search-按关键字搜索亚马逊商品接口),亚马逊API接口
How to make good use of data science?
Motianlun "high availability architecture" dry goods document sharing (including 124 Oracle, MySQL and PG materials)
Implementing LDAP proxy service with haproxy + keepalive