当前位置:网站首页>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 .
边栏推荐
- Redis (I) -- getting started with redis (1) -- redis introduction, installation and startup, and common configurations
- MySQL,MVCC详解,快照读在RC、RR下的区别
- 89. (cesium article) cesium aggregation diagram (custom picture)
- 小型图书馆项目总结
- 唯品会商品详情API接口(item_get-获得唯品会商品详情接口),唯品会详情API接口
- How to prepare samples for application of color coated steel sealing plates to BS 476-3?
- 唯品会关键词搜索API接口(item_search-按关键字搜索唯品会商品API接口),唯品会API接口
- 亚马逊关键词搜索API接口(item_search-按关键字搜索亚马逊商品接口),亚马逊API接口
- leetcode:724. Find the central subscript of the array
猜你喜欢

Yolov6 training your own data record +yolov5 comparison test

STM32 minimum system construction (schematic diagram)
![[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

Autodesk Revit 2023 software installation package download and installation tutorial

CLI tool foundation of ros2 robot f1tenth

Star ring technology data security management platform defender heavy release

尚硅谷实时数据仓库项目(阿里云实时数仓)

cout 不明确问题
![The inadvertently discovered [tidb cache table] can solve the read / write hotspot problem](/img/96/b1595b9d2b008b353765caa68fdd3c.png)
The inadvertently discovered [tidb cache table] can solve the read / write hotspot problem

Structure the fifth operation of the actual camp module
随机推荐
How do new shareholders open accounts online? Is it safe to open an account online?
Go standard library context package: data, cancellation signal, deadline and other related operations between a single request and multiple goroutines and the request domain
Change detection and batch update
STM32 and gd32 notes
Is it appropriate to apply silicone paint to American Standard UL 790 class a?
I want to register my stock account online. How do I do it? In addition, is it safe to open a mobile account?
Layer 3 loop brought by route Summary - solution experiment
C. Where‘s the Bishop?
Bs-gx-017 online examination management system based on SSM
A. Beat The Odds
LeetCode 1. Sum of two numbers
Wechat public account development, send message reply text
Detailed explanation of key points in implementing MES system in Enterprises
Vipshop Keyword Search API interface (item_search- search vipshop commodity API interface by keyword), vipshop API interface
唯品会关键词搜索API接口(item_search-按关键字搜索唯品会商品API接口),唯品会API接口
Which securities to choose for securities account opening? In addition, is it safe to open an account online now?
什么是 SYN 洪水攻击?如何防护?
A. Marathon
Amazon Product details API interface - (item_get get Amazon Product details interface), Amazon details API interface
澳洲要求PVC 塑料片符合AS/NZS 1530.3 火焰蔓延指数为0吗?