Golang interface application scenario usage details

Interface application scenarios


For beginners, it is not too difficult to understand the concept of interfaces. What is difficult is not knowing when to use interfaces. Here are a few application scenarios:

1. If the United States now wants to manufacture bombers and armed helicopters, experts only need to determine the functions/specifications required by the aircraft, and then let others implement them.

If you want to write a structure, there are some methods in the structure. It is definitely not good for a programmer to write these methods. If you want to get everyone involved, you can ask the project manager. The project manager can define an interface. Everyone can Just implement this interface and you're done. In this way, the progress of the management project can be well controlled.

In fact, it is just to take the definition specification and let others implement the specification.

If you want to sort, you only need to implement this data interface. 

In fact, all methods that implement the interface interface are passed in. Then you can call the sort method in the package and pass in this type. 

There is a Sort function in the sort package. Sort accepts data. It is an interface. As long as the variables passed in implement all the methods in the interface and passed in, it will naturally sort it for you. When you implement the interface, the Sort function will automatically help you implement sorting.

If as shown below, you used email to send alarm information before, and later if you want to change to dingding to send alarms, then the code needs to be changed. 

//如果产生了一个告警,将告警发送给相关的人
//email sms weixin

type EmailSender struct {

}

type DingDingSender struct {
	
}

func (e *EmailSender) Send(msg string) error {
	fmt.Println(msg)
	return nil
}

func (d *DingDingSender) Send(msg string) error{
	fmt.Println(msg)
	return nil
}

func test(e *EmailSender,msg string)  {
	e.Send(msg)
}

func main()  {
	sender := new(EmailSender)
	sender.Send("email send")
}

If you need to modify it, please modify it as follows. You must modify all the codes related to email. If not, it will cause the compilation to fail.

In order to solve this problem, you can define interfaces. One feature of these structures is that they all have Send methods. In this way, you can define interfaces with only Send methods, and then the parameter types can use the interface type.

It would be much more convenient if the interface is defined. You only need to change one place, which is the place where the structure is instantiated.

type Sender interface {
	Send(string) error
}

	sender := new(DingDingSender)
	test(sender,"dingding send")
	
    sender1 := new(EmailSender)
	test(sender1,"email send")

2. Suppose there is a project manager who manages three programmers and develops a software function. In order to control and manage the software, the project manager can define some interfaces, which are then implemented by the programmers.

The project manager only needs to define the interface, and let the programmers abc complete the rest. In this way, the progress of software development can be controlled.

Notes and details


1) The interface itself cannot create an instance, but it can point to a variable (instance) of a custom type that implements the interface

type interfaceA interface {
	Say()
}

type Student struct {
}

func (*Student) Say() {
	fmt.Println("I am a student")
}

func main() {
	var i interfaceA
	student := new(Student)
	i = student //结构体变量实现了接口的方法
	i.Say()
}

2) All methods in the interface have no method body, that is, they are all methods without implementation.

3) In Golang, a custom type needs to implement all methods of an interface. We say that this custom type implements the interface.

4) Only when a custom type implements an interface can an instance (variable) of the custom type be assigned to the interface type.

5) As long as it is a custom data type, you can implement the interface, not just the structure type.

type interfaceA interface {
	Say()
}

type integer int

func (i *integer) Say() {
	fmt.Println("i地址为:", &i, "i的值为:", *i)
}

func main() {
	var i interfaceA
	var a integer = 5
	a.Say()
}

6) A custom type can implement multiple interfaces

7) There cannot be any variables in the Golang interface

As you can see above, it is different from traditional oop. It implements this interface based on methods. In Java, these are implemented explicitly, and you must explicitly specify which interface to implement. Golang does not care which interface is implemented, only which method is implemented.

8) An interface (such as A interface) can inherit multiple other interfaces (such as B and C interfaces). In this case, if you want to implement A interface, you must also implement all the methods of B and C interfaces.

You can see that there are two methods in the A interface, which is equivalent to inheriting these two interfaces. Then you need to implement both the methods in the inherited interface and your own methods.

9) The interface type defaults to a pointer (reference type). If it is used without initializing the interface, nil will be output ( when passing in parameters, such as structures, what is passed in is not a value type, but a reference type. , that is, &struct{} )

10) The empty interface interface does not have any methods, so all types implement the empty interface (the empty interface is actually a data type. Variables of any data type can be assigned to the empty interface. If the parameter is a formal parameter of the empty interface, then means any data type can be accepted)

As you can see below, the structure implements the empty interface by default. Other types are also available, including string integers.

type T interface {
}

type student struct {
	age  int
	name string
}

func main() {
	s := &student{
		age:  10,
		name: "lucas",
	}
	var t T = s
	fmt.Println(reflect.TypeOf(t))
	fmt.Println(t)

	var t3 interface{} = s
	fmt.Println(t3)
}

*main.student
&{10 lucas}
&{10 lucas}

Any variable can be assigned to the empty interface

Guess you like

Origin blog.csdn.net/qq_34556414/article/details/133605963