Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the empty interface and usage scenario in object-oriented Go language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

What this article shares with you is about the empty interfaces and usage scenarios in the object-oriented Go language. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Students familiar with Java should know that in this object-oriented programming language, which claims to be the purest bloodline, "everything is object", and all classes inherit from the ancestral class "Object", so Object type variables can point to instances of any class.

Go language breaks the concept of inheritance between classes in traditional object-oriented programming, but realizes the reuse of methods and attributes through combination, so there is no similar inheritance tree, so there is no so-called ancestral class, and the relationship between classes and interfaces is no longer enforced by the implements keyword, so the object-oriented programming of Go language is very flexible. We know that in Go, the implementation relationship between types and interfaces is inferred at compile time through methods implemented by classes. If we define an empty interface, it is obvious that all types implement this interface, and then we can point to any type through this empty interface, thus implementing functions similar to those undertaken by the Object class in Java, and obviously the empty interface implementation of Go is more concise. This can be done with a simple interface {} literal quantity, and the basic types can be declared, and the same function can only be converted by boxing in Java.

Let's take a look at an example of using the interface {} empty interface.

We can point it to the basic type:

Var v1 interface {} = 1 / assign int type to interface {} var v2 interface {} = "academic gentleman" / / assign string type to interface {} var v3 interface {} = true / / assign bool type to interface {}

You can also point it to a compound type:

Var v4 interface {} = & v2 / assign * interface {} type (pointer) to interface {} var v5 interface {} = [] int {1, 2, 3} / assign slice type to interface {} var v6 interface {} = struct {/ / assign custom type to interface {} id int name string} {1, "academy"}

The most typical use scenario of empty interface interface {} is to declare that the function supports any type of parameters. For example, the print function in the Go standard library fmt is implemented in this way:

Func Printf (fmt string, args... interface {}) func Println (args... interface {}).

In addition, we can also achieve type determination and conversion based on an empty interface:

Var myarr = [3] int {1,2,3} value, ok: = interface {} (myarr). ([3] int)

Because interface {} can point to any type, we first convert myarr to an empty interface type, and then assert whether x is a T type through the x. (T) expression, where T corresponds to [3] int, and the expression has two return values. If x type is T, the value of ok is the value of true,value converted to type T; otherwise, the value of ok is the null value of type T.

In addition, sometimes you may see an empty structure type definition: struct {}, which represents an empty class that does not have any properties or methods. There is only one instance value of this type, that is, struct {} {}. This value will always be stored in a Go program, and the memory space is 0. When we use channel as a medium to transmit simple signals in concurrent programming, It is best to use the struct {} type to declare.

These are the empty interfaces and usage scenarios in the object-oriented Go language. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report