Home  >  Article  >  Backend Development  >  An article to help you understand the basic interface of Go language (Part 1)

An article to help you understand the basic interface of Go language (Part 1)

Go语言进阶学习
Go语言进阶学习forward
2023-07-21 14:11:15768browse

What is an interface?

The interface here does not mean that kind of slot That kind of interface, just get together and be done with it.

In various languages, when interfaces are mentioned, they usually refer to a specification, and then specific objects implement the details of this specification.

<br>

The interfaces used in this article are mainly constraint interfaces, and there is also a storage interface.

Note:

In Go, an interface is a type , an abstract type that has only methods and no attributes.

<br>

Why interfaces are needed

When we talk about structures, the structure of the basics of the Go language (Spring), the structure of the basics of the Go language (Summer) , Structure of the Basics of Go Language (Autumn Chapter), mentioned the concept of inheritance, Go completes inheritance through structures.

<br>

Review inheritance

Car structure

//车
type Car struct {
    Brand  string //车品牌
    CarNum string //车牌号
    Tyre   int    //轮胎个数
}


//给车绑定一个方法,说明车的基本信息
func (this *Car) carInfo() {
    fmt.Printf("品牌:%s,车牌号:%s,轮胎个数:%d\n", this.Brand, this.CarNum, this.Tyre)
}

The car structure has four attributes and a method to display car (carInfo) information. <br>

<br>

##BMW car

//宝马车
type BMWCar struct {
    //*Car和Car基本没有区别,一个存的是整个结构体,一个存的是结构体地址,用法大同小异
    *Car //这就表示继承了Car这个结构体
}

BYD车<br>

//比亚迪车
type BYDCar struct {
    *Car
}

main code<br>

func main() {
    //一个宝马对象
    var bmw1 = BMWCar{&Car{
        Brand:  "宝马x8",
        CarNum: "京666",
        Tyre:   4,
    }
}
    //一个比亚迪对象
    var byd1 = BYDCar{&Car{
        Brand:  "比亚迪L3",
        CarNum: "京111",
        Tyre:   4,
    }
}
    //因为 BMWCar 和 BYDCar 都继承了Car,所以都有carInfo这个方法
    bmw1.carInfo()
    byd1.carInfo()
}

execution result<br>

An article to help you understand the basic interface of Go language (Part 1)

通过回顾,我们可以发现,车,应该作为一个基本的概念。<br>

上述Car结构体似乎显示了车的属性,其实是不太对的。

车就是一个抽象的概念,电瓶车是车,小轿车也是车,大卡车也是车。

这些车至少有一个统一的功能,那就是跑,但是像几个轮胎了,什么品牌了。

应该是属于自己的,不再是属于Car这个抽象的概念中了,所以,这时候用接口会更好。

<br>

定义接口

车接口

type Car interface {
    //车会跑
    Run(speed int)
    //车需要加油
    Refuel(oil int)
    //车需要转弯
    Wheel(direction string)
}

假设车,至少有这三个动作,不管任何结构体,只要实现了Car里面的所有方法,就代表它一定是一个车。<br>

<br>

宝马车

//宝马车
type BMWCar struct {
    Owner  string //车主
    Brand  string //车品牌
    CarNum string //车牌号
}
//构造方法
func NewBMWCar(owner string, brand string, carNum string) *BMWCar {
    return &BMWCar{Owner: owner, Brand: brand, CarNum: carNum}
}


func (this *BMWCar) Run(speed int) {
    fmt.Printf("我是 %s,我的车是 %s,我车牌号为 %s,我正在以 %d 速度行驶\n", this.Owner, this.Brand, this.CarNum, speed)
}


func (this *BMWCar) Refuel(oil int) {
    fmt.Printf("老板,加%d升油\n", oil)
}


func (this *BMWCar) Wheel(direction string) {
    fmt.Printf("我正在%s转弯\n", direction)
}

电瓶车<br>

<br>
//电瓶车
type Electromobile struct {
    Owner string //车主
    Brand string //车品牌
}


func NewElectromobile(owner string, brand string) *Electromobile {
    return &Electromobile{Owner: owner, Brand: brand}
}
func (this *Electromobile) Run(speed int) {
    fmt.Printf("我是 %s,我的车是 %s,我正在以 %d 速度行驶\n", this.Owner, this.Brand,, speed)
}


func (this *Electromobile) Refuel(oil int) {
    fmt.Printf("你妹的,你电动车加啥油...\n")
}


func (this *Electromobile) Wheel(direction string) {
    fmt.Printf("我正在%s转弯\n", direction)
}

这里是有区别的,电瓶车没有属性CarNum,但是仍然实现了Car接口的所有方法,所以电瓶车在代码上,仍然是车。

main

func main() {
    var 张三的车 Car
    张三的车 = NewBMWCar("张三", "宝马6", "京666")
    张三的车.Run(80)
    张三的车.Refuel(20)
    张三的车.Wheel("左")


    var 李四的车 Car
    李四的车 = NewElectromobile("李四", "小刀电动车")
    李四的车.Run(40)
    李四的车.Refuel(0)
    李四的车.Wheel("左")
}

第2行代码和第8行代码,变量类型是Car接口类型,但是在赋值时,确是其他类型。<br>

Go是强类型语言,为什么类型不一致,还可以赋值,那执行结果会出问题吗???

执行结果

An article to help you understand the basic interface of Go language (Part 1)

但是我们发现执行结果是没问题的。<br>

But why is it that the variable types are inconsistent, but it can still be assigned and an error is reported every time?

Our above code can determine BMW car and battery carCompletely implements all the methods in the Car interface.

So it can be understood that Car# is their father. Of course, you can use their father to receive your son. Yes.

<br>

##One structure implements multiple interfaces

The following code has no actual Meaning, it's all about grammar for grammar's sake.

接口代码

//跑接口
type Runer interface {
  Run()
}


// 跳接口
type Jumper interface {
  Jump()
}

结构体代码

//袋鼠结构体
type Roo struct {
  Name string
}


func (this *Roo) Jump() {
  fmt.Println("我是袋鼠,我会跳")
}


func (this *Roo) Run() {
  fmt.Println("我是袋鼠,我会跑")
}

这个结构体同时实现了两个结构,一个是Runer,一个是Jumper

<br>

main代码

func main() {
  var runner Runer
  var jumper Jumper


  runner = &Roo{Name: "袋鼠"}
  jumper = &Roo{Name: "袋鼠"}


  runner.Run()
  jumper.Jump()
}

Roo既然实现了两个接口,自然两个接口都可以接收Roo这个结构体。

执行结果

An article to help you understand the basic interface of Go language (Part 1)

<br>

接口嵌套<br>

接口嵌套这个有点像组合,比如有等这些操作。

例如一个动物,因该是要有这些操作的,那这个动物应该也是一个接口。

并且把这些动作都拿过来才对。

<br>

接口示例代码

//跑接口
type Runer interface {
  Run()
}


// 跳接口
type Jumper interface {
  Jump()
}


//动物接口,继承了 跑 和 跳 
type Animal interface {
  Runer
  Jumper
}

结构体代码

//袋鼠结构体,实现了跑和跳
type Roo struct {
  Name string
}


func (this *Roo) Jump() {
  fmt.Println("我是袋鼠,我会跳")
}


func (this *Roo) Run() {
  fmt.Println("我是袋鼠,我会跑")
}

main代码

func main() {
  var animal Animal


  animal = &Roo{Name: "袋鼠"}
  animal = &Roo{Name: "袋鼠"}


  animal.Run()
  animal.Jump()
}

执行结果

An article to help you understand the basic interface of Go language (Part 1)

<br>

Summary<br>

We have learned the basic interfaces of Go above, mainly The difference between interfaces and inheritance, one structure implements multiple interfaces,interface nesting .

It may not be easy to understand, but you must try it and persist!

The above is the detailed content of An article to help you understand the basic interface of Go language (Part 1). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete