当前位置:网站首页>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 .
边栏推荐
- Simple analysis of wieshark packet capturing MySQL protocol
- Use of golang gopsutil Library: process and system resource monitoring (CPU, memory, disk, etc.)
- 【ROS进阶篇】第二讲 自定义头、源文件封装
- MySQL,MVCC详解,快照读在RC、RR下的区别
- leetcode:724. Find the central subscript of the array
- 生产环境AIX小机报错B6267342问题处理
- Information available from radar echo
- 唯品会关键词搜索API接口(item_search-按关键字搜索唯品会商品API接口),唯品会API接口
- 澳洲要求PVC 塑料片符合AS/NZS 1530.3 火焰蔓延指数为0吗?
- Goahead webserver migration
猜你喜欢

Summer Challenge harmonyos ark development framework arkui streamer button effect

美国隧道法ASTM E84 表面阻燃测试

细说GaussDB(DWS)复杂多样的资源负载管理手段

2022 openvino DevCon unveils secrets! Intel and many partners deepen the construction of developer ecology and release the innovation potential of AI industry

ASP. Net cross page submission (button control page redirection)

Viewing technological changes through Huawei Corps (V): smart Park

ASP动态创建表格 Table

STM32 minimum system construction (schematic diagram)

Cout ambiguous problem

Realization of graduation project topic selection system based on JSP
随机推荐
I want to register my stock account online. How do I do it? In addition, is it safe to open a mobile account?
Automatic reply of wechat bulletin number intelligent reply with Turing robot
阿里巴巴店铺的所有商品API接口(item_search_shop-获得店铺的所有商品接口),阿里巴巴API接口
Is it appropriate to apply silicone paint to American Standard UL 790 class a?
亚马逊商品详情API接口-(item_get-获得AMAZON商品详情接口),亚马逊详情API接口
CSDN failed to replicate problem
DevCloud加持下的青软,让教育“智”上云端
Houdini graphic notes: VAT (3.0) import ue4/5 setup wizard [official document translation]
C. Most Similar Words
Report delivery engineer
Small library project summary
API interfaces for all products in Alibaba stores (item_search_shop- obtain all product interfaces in the store), Alibaba API interfaces
89. (cesium article) cesium aggregation diagram (custom picture)
Reading notes on how to connect the network - LAN on the server side (4)
Top ten questions for senior Performance Test Engineer
In the shop project, implement a menu (add, delete, modify and query)
Taro applet enables wxml code compression
Type of radar
MySQL,MVCC详解,快照读在RC、RR下的区别
澳洲要求PVC 塑料片符合AS/NZS 1530.3 火焰蔓延指数为0吗?