Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Go language interface

Shulou Source: shulou.com Published: 2022-05-31 13:49:26 09月16日 Update

This article introduces the knowledge of "how to use the Go language interface". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Example

Package main

Import (

"fmt"

)

Type Phone interface {

Call ()

}

Type NokiaPhone struct {

}

Func (nokiaPhone NokiaPhone) call () {

Fmt.Println ("I am Nokia, I can call you!")

}

Type IPhone struct {

}

Func (iPhone IPhone) call () {

Fmt.Println ("I am iPhone, I can call you!")

}

Func main () {

Var phone Phone

Phone = new (NokiaPhone)

Phone.call ()

Phone = new (IPhone)

Phone.call ()

}

In the above example, we define an interface Phone with a method call () in it. Then we define a Phone type variable in the main function and assign it to NokiaPhone and IPhone, respectively. Then call the call () method, and the output is as follows:

I am Nokia, I can call youthful I am iPhone, I can call you! the content of "how to use Go language interface" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Interfaces languages contents methods more knowledge output practicality learning next examples functions variables dilemmas examples facts situations articles cases types Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Shulou Tech Info Huawei Apple