What is the relationship between types and interfaces in Go language

青灯夜游
Release: 2023-01-07 11:53:57
Original
3878 people have browsed it

In the Go language, there is a one-to-many and many-to-one relationship between types and interfaces. A type can implement multiple interfaces at the same time, and the interfaces are independent of each other and do not know each other's implementation. Multiple types can also implement the same interface: the methods of an interface do not necessarily need to be fully implemented by a type. The methods of the interface can be implemented by embedding other types or structures in the type. In other words, users do not care whether the method of an interface is fully implemented through one type, or whether it is implemented through multiple structures embedded in a structure and pieced together.

What is the relationship between types and interfaces in Go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

The relationship between Go language types and interfaces

In Go language, there is a one-to-many and many-to-one relationship between types and interfaces. These common concepts will be listed below to facilitate readers to understand the implementation relationship between interfaces and types in complex environments.

A type can implement multiple interfaces

A type can implement multiple interfaces at the same time, and the interfaces are independent of each other and do not know each other's implementation.

Two programs on the network exchange data through a two-way communication connection. One end of the connection is called a Socket. Socket can read and write data at the same time, this feature is similar to a file. Therefore, during development, the reading and writing features of both files and Sockets are abstracted into independent reader and writer concepts.

Sockets, like files, also need to release resources after use.

Use the interface to describe the features of Socket that can write data and need to be closed. Please refer to the following code:

type Socket struct {
}
func (s *Socket) Write(p []byte) (n int, err error) {
    return 0, nil
}
func (s *Socket) Close() error {
    return nil
}
Copy after login

The Write() method of the Socket structure implements the io.Writer interface:

type Writer interface {
    Write(p []byte) (n int, err error)
}
Copy after login

At the same time, the Socket structure also implements the io.Closer interface:

type Closer interface {
    Close() error
}
Copy after login

Using the code of the Writer interface implemented by Socket, there is no need to know whether the implementer of the Writer interface has the characteristics of the Closer interface. Similarly, the code using the Closer interface does not know that the Socket has implemented the Writer interface, as shown in the following figure.

What is the relationship between types and interfaces in Go language
Figure: The use and implementation process of the interface

The Writer interface and Closer interface codes implemented using the Socket structure in the code are as follows:

// 使用io.Writer的代码, 并不知道Socket和io.Closer的存在
func usingWriter( writer io.Writer){
    writer.Write( nil )
}
// 使用io.Closer, 并不知道Socket和io.Writer的存在
func usingCloser( closer io.Closer) {
    closer.Close()
}
func main() {
    // 实例化Socket
    s := new(Socket)
    usingWriter(s)
    usingCloser(s)
}
Copy after login

usingWriter () and usingCloser() are completely independent. They do not know the existence of each other, nor do they know that the interface they use is implemented by Socket.

Multiple types can implement the same interface

The methods of an interface do not necessarily need to be completely implemented by one type. The methods of the interface can be embedded in other types in the type. type or structure. In other words, users do not care whether the method of an interface is fully implemented through one type, or whether it is implemented through multiple structures embedded in a structure and pieced together.

The Service interface defines two methods: one is to start the service (Start()), and the other is to output the log (Log()). Use the GameService structure to implement Service. GameService's own structure can only implement the Start() method, and the Log() method in the Service interface has been implemented by a logger (Logger) that can output logs. There is no need to encapsulate GameService. Or implement it again. Therefore, choosing to embed Logger into GameService can avoid code redundancy and simplify the code structure to the greatest extent. The detailed implementation process is as follows:

// 一个服务需要满足能够开启和写日志的功能
type Service interface {
    Start()  // 开启服务
    Log(string)  // 日志输出
}
// 日志器
type Logger struct {
}
// 实现Service的Log()方法
func (g *Logger) Log(l string) {
}
// 游戏服务
type GameService struct {
    Logger  // 嵌入日志器
}
// 实现Service的Start()方法
func (g *GameService) Start() {
}
Copy after login

The code description is as follows:

  • Line 2 defines the service interface. A service needs to implement the Start() method and the log method.

  • Line 8 defines the logger structure that can output logs.

  • In line 12, add the Log() method to the Logger and implement the Log() method of the Service.

  • Line 17 defines the GameService structure.

  • Line 18, embed the Logger logger in GameService to implement the logging function.

  • Line 22, GameService’s Start() method implements Service’s Start() method.

At this point, instantiate GameService and assign the instance to Service. The code is as follows:

var s Service = new(GameService)
s.Start()
s.Log(“hello”)
Copy after login

s You can use the Start() method and Log() method, Among them, Start() is implemented by GameService, and the Log() method is implemented by Logger.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of What is the relationship between types and interfaces in Go language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template