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

The introduction and function of the interface of Go language

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Go language interface introduction and what is the role, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

An interface is a collection of methods (canonical behavior)

In the object-oriented domain, interfaces are generally defined as follows: interfaces define the behavior of an object and regulate the behavior of subclass objects.

Interfaces in Go are non-intrusive interfaces (no interface, no code impact), intrusive interfaces (interface gone, subclass error reported)

Go is also a duck type. For example, I now have a duck class, which contains speak method and run method. As long as the subclass implements speak and run, I think the subclass is a duck. As long as there are these two methods in the subclass, you are the duck. With these two methods, you are the duck. He deduces from the bottom up that as long as there is something in you, it is regarded as inheriting your interface.

1. The purpose of the interface

An interface is a type

/ / Duck defines a duck interface type Duck interface {speak () run ()} / / WhiteDuck defines a white duck substructure type WhiteDuck struct {name string age int sex string} / / BlackDuck defines a black duck substructure type BlackDuck struct {name string age int sex string} / / binds all the methods in the interface between the white duck and the black duck Call to implement this interface / / Let the white duck implement the Duck interface func (w WhiteDuck) speak () {fmt.Println ("White duck quack, its name is", w.name)} func (w WhiteDuck) run () {fmt.Println ("White duck walk slowly, its name is", w.name)} / / Let the black duck implement the Duck interface func (b BlackDuck) speak () {fmt.Println ("black duck quack. Its name is ", b.name)} func (b BlackDuck) run () {fmt.Println (" Black Duck walking askew, its name is ", b.name)} func main () {var duck Duck duck = WhiteDuck {" Xiaobai ", 15," male "} / / assign my object to an interface type, you can achieve polymorphic effect fmt.Println (duck) / / duck now he is an interface. It can only take methods, not properties. Duck.speak () duck.run ()}

/ / output:

{rookie 15 male}

The white duck quacks. Its name is Xiaobai.

The white duck walks slowly. Its name is Xiaobai.

2. Type assertion

Used to extract the underlying value of the interface, that is, convert the interface type to struct, attributes, and have your own methods.

Func main () {var duck Duck = WhiteDuck {"Xiaobai", 15, "male"} / / asserts that it is of WhiteDuck type value, ok: = duck. (WhiteDuck) / / asserted successfully, ok=true Value is the WhiteDuck structure object fmt.Println (value) / / output: {rookie 15 male} fmt.Println (value.name) / / output: rookie fmt.Println (ok) / / output: true / / assertion failed. Ok1=false,value1 is a null value of type BlackDuck. Because value1 is not assigned, ok1: = duck. (BlackDuck) fmt.Println (value1) / / output: {0} fmt.Println (ok1) / / output: false} 3, type selection

(via Type Switch)

Used to compare the specific type of an interface with the type specified by many case statements.

Func main () {var duck Duck = WhiteDuck {"Xiaobai", 15 "male"} test (duck)} func test (duck Duck) {switch value: = duck. (type) {case WhiteDuck: fmt.Println (value.name) fmt.Println ("I am a white duck") case BlackDuck: fmt.Println (value.name) fmt.Println ("I am a black duck") default: fmt.Println (value) fmt.Println ("I am a duck")} 4, empty interface

There is no method, and all data types implement an empty interface

Type Empty interface {} / / empty interface func main () {var an int = 10 var b string = "XiaoYang" var c [3] int var e Empty / / e is an empty interface type and can accept any data type e = an e = b e = c / / so I need to select its type back / / normally I can only accept Empty type But a b c is not test (a) / / output of type Empty: I am int 10 test (b) / / output: I am the string XiaoYang test (c) / / output: I am the array [000]} / / if this is not an empty interface For example, Duck, as long as all the data types of the Duck interface are implemented, you can pass func test (b Empty) {switch vblog. (type) {case string: fmt.Println ("I am a string", v) case int: fmt.Println ("I am int", v) case [3] int: fmt.Println ("I am an array", v)} 5, anonymous empty interface.

An empty interface without a name, usually used in formal parameters

Func main () {var duck Duck = WhiteDuck {"Xiaobai", 15, "male"} test (10) test ("XiaoYang") test (duck)} / / this is called anonymous empty interface, and all data types can be passed to it. If you want to use the original structure, you still need to select the type back to use func test (b interface {}) {fmt.Println (b)} 6, Implement multiple interfaces / / Duck define a duck interface type Duck interface {speak () run ()} type Animal interface {eat () sleep ()} / / WhiteDuck define a white duck structure type WhiteDuck struct {name string age int sex string} / / Let the white duck implement the Duck interface and also implement the Animal interface func (w WhiteDuck) speak () {fmt.Println ("white duck quack" Its name is ", w.name)} func (w WhiteDuck) run () {fmt.Println (" White duck walking slowly, its name is ", w.name)} func (w WhiteDuck) eat () {fmt.Println (" White duck eats, its name is ", w.name)} func (w WhiteDuck) sleep () {fmt.Println (" White duck sleeps ") Its name is ", w.name)} func main () {var w WhiteDuck = WhiteDuck {} var an Animal var d Duck / / so my w can be given either an or d / /, but once transferred to an interface, I can only use the methods of that interface. Their own properties and methods need type assertions before they can be given to a using a = w / / w, then a can only call the method a.sleep () a.eat () d = w / / w of the Animal interface to d Then a can only call the method d.run () d.speak ()} 7 of the Duck interface. Interface nesting type Duck interface {Animal / / Duck nested Animal interface speak () run ()} type Animal interface {eat () sleep ()} type WhiteDuck struct {name string age int sex string} / / so that the white duck implements the Duck interface as well as the Animal interface func (w WhiteDuck) speak () {fmt.Println ("white duck quack" Its name is ", w.name)} func (w WhiteDuck) run () {fmt.Println (" White duck walking slowly, its name is ", w.name)} func (w WhiteDuck) eat () {fmt.Println (" White duck quack, its name is ", w.name)} func (w WhiteDuck) sleep () {fmt.Println (" White duck walking slowly. Its name is ", w.name)} func main () {var an Animal var d Duck var w WhiteDuck = WhiteDuck {} / / w can give a You can also give d a = w / / but a can only call two methods in Animal, a.sleep () a.eat () d = w / / d, but can call four methods in Duck and Animal, d.sleep () d.eat () d.speak () d.run ()} 8, The interface zero value func main () {var an Animal / / nil means that it is a reference type / / its internal representation has already told us. There are two values stored in it, one is its type, the other is the pointer to the specific value fmt.Println (a) / / output:} 9, the difference between make and new type WhiteDuck struct {name string sex string age int} func main () {var per1 * WhiteDuck = new (WhiteDuck) / / new returns the pointer to this type / or the address where I took the WhiteDuck Assign to per2 var per2 = & WhiteDuck {} fmt.Println (per1) / / output: & {0} fmt.Println (per2) / / output: & {0} var per3 = make ([] int, 3 4) / / make is the specific creation reference type / / new is to create a pointer to this type var per4 = new ([] int) / / is a pointer to the slice type fmt.Println (per3) / / output: [000] fmt.Println (per4) / output: & []} is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Development

Wechat

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

12
Report