How to use Go language interface
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!