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 function of the interface of Go language

2025-01-17 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.

Share To

Database

Wechat

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

12
Report