Golang Introduction Series (iv) How to understand the interface interface

Source: Internet
Author: User

A lot of the basics of Go language, including the installation of Go environment, the grammar of Go language, and other interested friends, can first look at the previous article. Www.cnblogs.com/zhangweizhong/category/1275863.html

Start writing the Go code today and talk about how to understand the interface interface.

1. What is the interface interface?

Interface is one of the basic features of the go language. Can be understood as a type of specification or convention. It is not the same as java,c#, do not need to display instructions to implement an interface, it does not inherit or subclass or "implements" keyword, only through the form of the Convention, the implicit implementation of interface in the method can be. As a result, the interface in Golang makes coding more flexible and easy to scale.

How to understand interface in the go language? Just remember the following three points:

1. Interface is a collection of method declarations
2. Any type of object implements all the methods declared in the interface interface, indicating that the type implements the interface.
3. Interface can be used as a data type, any object that implements the interface can assign a value to the corresponding interface type variable.

Attention:
A. Interface can be implemented by arbitrary objects, and a type/object can also implement multiple interface
B. Methods cannot be overloaded, such as Eat () eat (s string) cannot exist simultaneously


2. Interface implementation

"FMT"type Phone interface {call    ()}type nokiaphone struct {}func (nokiaphone nokiaphone) call () {    fmt. Println ("I am Nokia, I can call you!" )}type applephone struct {}func (iPhone applephone) call () {    fmt. Println ("I am Apple Phone, I can call you!" )}func Main () {    var phone phone    = new (nokiaphone)    phone.call ()    = New ( Applephone)    Phone.call ()}

3. Interface Query

If interface a implements all the methods in interface B, then a can be converted to a B interface.

if varName2, OK: = varName1. (interface2| typeName); OK {    // at this point the type of varName2 is changed from Interface1 to Interface2, or varName1 is not a typeName type variable  else
    {    // cannot convert interface, or VARNAME1 is not a variable of type TypeName

4. interface{} type

  interface{} 类型没有声明任何一个方法,俗称空接口。interface{} 在我们需要存储任意类型的数值的时候相当有用,有点类似于C语言的void*类型。

Package Mainimport (    "FMT") func printall (Vals []interface{}) {    for _, val:= range Vals {
   fmt. Println (val)    }}func Main () {    names:= []string{"Stanley", "David", "Oscar"}    vals:=  Make ([]interface{}, Len (names))    for I, V:= range names {        = v    }    printall ( Vals)}

However, it should be noted that []t cannot be directly assigned to []interface{}

        T: = []int{1234}        var s []  Interface{} = t

The following error is printed at compile time:

Cannot use T (Type []int) as type []interface {} in Assignment

At last

Above, a simple introduction to the interface features in Go language. I think for the go language, the design of the most subtle should be interface, interested, can study well.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.