What is the relationship between types and interfaces in Go language
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.
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 }
The Write() method of the Socket structure implements the io.Writer interface:
type Writer interface { Write(p []byte) (n int, err error) }
At the same time, the Socket structure also implements the io.Closer interface:
type Closer interface { Close() error }
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.
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) }
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() { }
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”)
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!

Go's "strings" package provides rich features to make string operation efficient and simple. 1) Use strings.Contains() to check substrings. 2) strings.Split() can be used to parse data, but it should be used with caution to avoid performance problems. 3) strings.Join() is suitable for formatting strings, but for small datasets, looping = is more efficient. 4) For large strings, it is more efficient to build strings using strings.Builder.

Go uses the "strings" package for string operations. 1) Use strings.Join function to splice strings. 2) Use the strings.Contains function to find substrings. 3) Use the strings.Replace function to replace strings. These functions are efficient and easy to use and are suitable for various string processing tasks.

ThebytespackageinGoisessentialforefficientbyteslicemanipulation,offeringfunctionslikeContains,Index,andReplaceforsearchingandmodifyingbinarydata.Itenhancesperformanceandcodereadability,makingitavitaltoolforhandlingbinarydata,networkprotocols,andfileI

Go uses the "encoding/binary" package for binary encoding and decoding. 1) This package provides binary.Write and binary.Read functions for writing and reading data. 2) Pay attention to choosing the correct endian (such as BigEndian or LittleEndian). 3) Data alignment and error handling are also key to ensure the correctness and performance of the data.

The"bytes"packageinGooffersefficientfunctionsformanipulatingbyteslices.1)Usebytes.Joinforconcatenatingslices,2)bytes.Bufferforincrementalwriting,3)bytes.Indexorbytes.IndexByteforsearching,4)bytes.Readerforreadinginchunks,and5)bytes.SplitNor

Theencoding/binarypackageinGoiseffectiveforoptimizingbinaryoperationsduetoitssupportforendiannessandefficientdatahandling.Toenhanceperformance:1)Usebinary.NativeEndianfornativeendiannesstoavoidbyteswapping.2)BatchReadandWriteoperationstoreduceI/Oover

Go's bytes package is mainly used to efficiently process byte slices. 1) Using bytes.Buffer can efficiently perform string splicing to avoid unnecessary memory allocation. 2) The bytes.Equal function is used to quickly compare byte slices. 3) The bytes.Index, bytes.Split and bytes.ReplaceAll functions can be used to search and manipulate byte slices, but performance issues need to be paid attention to.

The byte package provides a variety of functions to efficiently process byte slices. 1) Use bytes.Contains to check the byte sequence. 2) Use bytes.Split to split byte slices. 3) Replace the byte sequence bytes.Replace. 4) Use bytes.Join to connect multiple byte slices. 5) Use bytes.Buffer to build data. 6) Combined bytes.Map for error processing and data verification.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version
