GO Language interface Text

1.1 interface is concise
Interface (interface) is one of the most important features of golang. Interface types can define a set of methods, but these do not require reality
Now.And interface cannot contain any variables.
◼ interface is a collection of methods
◼ interface is a type and is a pointer type
◼ A more important role for interface is in the polymorphic implementation
◼ interface cannot contain any variables

1.2 interface Definition

type Interface Name interface {
	method1 (parameter list) Return Value List
	method2 (parameter list) Return Value List
	...
}

1.3 interface use

◼ Interfaces are used not only for structs, but also for custom types, variables, and so on.
◼ If an interface does not have any methods, we call it an empty interface. Since an empty interface has no methods, any structure will
Implicitly implemented an empty interface.
◼ To implement an interface, all methods within that interface must be implemented.

//Define interfaces
type Skills interface {
	Running()
	Getname() string
}
type Student struct {
	Name string
	Age int
}
// Implement Interface
func (p Student) Getname() string { //Implement the Getname method
	fmt.Println(p.Name)
	return p.Name
}
func (p Student) Running() { // Implementing the Running method
	fmt.Printf("%s running", p.Name)
}
// Use interface
func main() {
	var skill Skills
	var stu1 Student
	stu1.Name = "darren"
	stu1.Age = 34
	skill = stu1
	skill.Running() //Call Interface
}

1.4 interface Polymorphism

◼ interface in go language is a form of polymorphism, so-called polymorphism is a variety of forms of things
◼ The same interface, different types of implementations, can be invoked, and they all operate on the same interface

example:

// 2.4 interface Polymorphism

package main

import "fmt"

type Skills interface {
	Running()
	Getname() string
}

type Student struct {
	Name string
	Age  int
}

type Teacher struct {
	Name   string
	Salary int
}

func (p Student) Getname() string { //Implement the Getname method
	fmt.Println(p.Name)
	return p.Name
}

func (p Student) Running() { // Implementing the Running method
	fmt.Printf("%s running", p.Name)
}

func (p Teacher) Getname() string { //Implement the Getname method
	fmt.Println(p.Name)
	return p.Name
}

func (p Teacher) Running() { // Implementing the Running method
	fmt.Printf("\n%s running", p.Name)
}
func main() {
	var skill Skills
	var stu1 Student
	var t1 Teacher
	t1.Name = "king"
	stu1.Name = "darren"
	stu1.Age = 22
	skill = stu1
	skill.Running()
	skill = t1
	skill.Running()
}

Execution results:
darren running
king running

1.5 interface interface nesting

◼ The interfaces in the go language can be nested, which is understood as inheritance, and the subinterfaces have all the methods of the parent interface
◼ If this subinterface is used, all methods of the parent and child interfaces must be implemented
example:

// 2.5 interface interface nesting
package main

import "fmt"
type Skills interface {
	Running()
	// Running(is int)		// Function name is unique
	Getname() string
}

type Test interface {
	Sleeping()
	Skills //Inherit Skills
}
type Student struct {
	Name string
	Age  int
}

type Teacher struct {
	skill  Skills // Skll can only be used as a variable
	Name   string
	Salary int
}

func (p Student) Getname() string { //Implement the Getname method
	fmt.Println(p.Name)
	return p.Name
}

func (p Student) Running() { // Implementing the Running method
	fmt.Printf("%s running", p.Name)
}

func (p Teacher) Getname() string { //Implement the Getname method
	fmt.Println(p.Name)
	return p.Name
}

func (p Teacher) Running() { // Implementing the Running method
	fmt.Printf("\n%s running", p.Name)
}

func (p Teacher) Sleeping() { // Implement Sleeping Method
	fmt.Printf("\n%s Sleeping", p.Name)
}
func main() {
	var skill Skills
	var stu1 Student
	var t1 Teacher
	t1.Name = "king"
	stu1.Name = "darren"
	stu1.Age = 22
	skill = stu1
	skill.Running()
	skill = t1
	skill.Running()
	
	var test Test
	test = t1
	test.Sleeping()
	test.Running()
}

Execution results:
darren running
king running
king Sleeping
king running

1.6 interface interface combination

◼ The definition of the interface also supports composite inheritance

// Composite Inheritance of 2.6 Interface
package main

import "fmt"

// Odour
type Smellable interface {
	smell()
}
// Eatable
type Eatable interface {
	eat()
}
type Fruitable interface {
	Smellable
	Eatable
}
// Apples can smell and eat
type Apple struct{}

func (a Apple) smell() {
	fmt.Println("apple can smell")
}

func (a Apple) eat() {
	fmt.Println("apple can eat")
}

// Flowers can only smell
type Flower struct{}

func (f Flower) smell() {
	fmt.Println("flower can smell")
}

func main() {
	var s1 Smellable
	var s2 Eatable
	var apple = Apple{}
	var flower = Flower{}
	s1 = apple
	s1.smell()
	s1 = flower
	s1.smell()
	s2 = apple
	s2.eat()

	fmt.Println("\n Combinatorial Inheritance")
	var s3 Fruitable
	s3 = apple
	s3.smell()
	s3.eat()
}

1.7 interface type conversion

Since an interface is a generic type, we may not know which type it implements when we use it.
Basic data types have their own methods for type conversion, and interface types also have type conversion.

var s int
var x interface
x = s
y , ok := x.(int)
//Convert interface to int,ok can be omitted, but if omitted, conversion failures will result in errors.
true The conversion was successful. false switch views, And default values

1.8 interface Type Judgment

package main

import "fmt"

type Student struct {
	Name string
}

func TestType(items ...interface{}) {
	for k, v := range items {
		switch v.(type) {
		case string:
			fmt.Printf("type is string, %d[%v]\n", k, v)
		case bool:
			fmt.Printf("type is bool, %d[%v]\n", k, v)
		case int:
			fmt.Printf("type is int, %d[%v]\n", k, v)
		case float32, float64:
			fmt.Printf("type is float, %d[%v]\n", k, v)
		case Student:
			fmt.Printf("type is Student, %d[%v]\n", k, v)
		case *Student:
			fmt.Printf("type is Student, %d[%p]\n", k, v)
		}
	}
}

func main() {
	var stu Student
	TestType("darren", 100, stu, 3.3, &stu, true)
}

Execution results:
type is string, 0[darren]
type is int, 1[100]
type is Student, 2[{}]
type is float, 3[3.3]
type is Student, 4[0xc000040240]
type is bool, 5[true]

1.9 Interface variables pointing to pointers

package main

import "fmt"

type Rect struct {
	Width  int
	Height int
}

func main() {
	var a interface{}
	var r = Rect{50, 50}
	a = &r // Points to the structure pointer

	var rx = a.(*Rect) // Convert to pointer type
	r.Width = 100
	r.Height = 100
	fmt.Println("r:", r)
	fmt.Println("rx:", rx)
	fmt.Printf("rx:%p, r:%p\n", rx, &r)
}

Execution results:
r: {100 100}
rx: &{100 100}
rx:0xc0000160c0, r:0xc0000160c0

Tags: Go pointer interface Polymorphism

Posted by VirusDoctor on Sat, 05 Jun 2021 02:14:09 +0930