In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what is the function of the interface of Go language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the function of the interface of Go language"?
Go language-Interface
In the go language, an interface type always represents the behavior of a certain type (that is, all the types that implement it). A declaration of an interface type usually contains the keyword type, the type name, the keyword interface, and several method declarations wrapped in curly braces. Examples are as follows:
Type Animal interface {Grow () Move (string) string}
Implementation interface
If the collection of methods owned by a data type contains the implementation of all method declarations in an interface type, it can be said that the data type implements that interface type.
You may have realized that we do not need to declare which interface it implements in a data type. As long as the condition that the method set is its superset is satisfied, the "implementation" relationship is established. This is a typical non-intrusive interface implementation.
Package main import ("fmt") / / defines an interface type People interface {ReturnName () string} / / defines a structure type Student struct {Name string} / / A method of defining a structure. / / this method is the same as all the methods of the interface People. At this point, it can be directly considered that the structure Student implements the interface People func (s Student) ReturnName () string {return s.Name} func main () {cbs: = Student {Name: "brown alpaca"} var a People a = cbs name: = a.ReturnName () fmt.Println (name) / / output "brown alpaca"}
How to test whether the interface has been implemented
H, ok: = a. (People)
This approach is called a type assertion expression.
A. (People) can be evaluated in two ways:
The first result is the value of the converted target type (in this case, Student).
The second result is a sign of whether the conversion operation is successful or not.
Null interface interface {}
An empty interface is an interface that does not contain any methods. Because of this, all data types implement empty interfaces. Just like the Object class in Java.
Therefore, an empty interface can store any type of data.
At this point, I believe you have a deeper understanding of "what is the role of the interface of the Go language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.