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!

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.

Golang is suitable for rapid development and concurrent programming, while C is more suitable for projects that require extreme performance and underlying control. 1) Golang's concurrency model simplifies concurrency programming through goroutine and channel. 2) C's template programming provides generic code and performance optimization. 3) Golang's garbage collection is convenient but may affect performance. C's memory management is complex but the control is fine.

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

C is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.

Golang excels in practical applications and is known for its simplicity, efficiency and concurrency. 1) Concurrent programming is implemented through Goroutines and Channels, 2) Flexible code is written using interfaces and polymorphisms, 3) Simplify network programming with net/http packages, 4) Build efficient concurrent crawlers, 5) Debugging and optimizing through tools and best practices.

The core features of Go include garbage collection, static linking and concurrency support. 1. The concurrency model of Go language realizes efficient concurrent programming through goroutine and channel. 2. Interfaces and polymorphisms are implemented through interface methods, so that different types can be processed in a unified manner. 3. The basic usage demonstrates the efficiency of function definition and call. 4. In advanced usage, slices provide powerful functions of dynamic resizing. 5. Common errors such as race conditions can be detected and resolved through getest-race. 6. Performance optimization Reuse objects through sync.Pool to reduce garbage collection pressure.

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Confused about the sorting of SQL query results. In the process of learning SQL, you often encounter some confusing problems. Recently, the author is reading "MICK-SQL Basics"...


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor