Golang Getting Started Series 4: How to Understand the Interface

I talked a lot about the basics of the Go language, including the installation of the go environment, the syntax of the go language, etc. If you are interested, you can first look at the previous article. http://quickintro123.com/index.php/category/golang/

Today I officially started writing Go code, and talked about how to understand the interface .

1. What is the interface

interface is one of the basic characteristics of the GO language. Can be understood as a type of specification or convention. It is not the same as Java or C #. It does not need to show that it implements an interface. It does not have inheritance or subclasses or the "implements" keyword. It just implements the methods in the interface implicitly by convention. Therefore, interfaces in Golang make coding more flexible and extensible.

How to understand the interface in 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 , it means 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.

Note:

a. Interface can be implemented by any object, and a type / object can also implement multiple interfaces
b. Methods cannot be overloaded, such as eat () eat (s string) cannot exist at the same time

2. Interface implementation

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 ApplePhone struct {
}

func (iPhone ApplePhone) call() {
    fmt.Println("I am Apple Phone, I can call you!")
}

func main() {
    var phone Phone
    phone = new(NokiaPhone)
    phone.call()

    phone = new(ApplePhone)
    phone.call()
}

3. interface query

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

 if varName2, ok := varName1.(interface2|typeName); ok {
    // At this time, the type of varName2 is changed from interface1 to interface2, or varName1 is not a variable of typeName
  } else {
    // Cannot convert interface, or varName1 is not a variable of typeName

4. The interface {} type

The interface {} type does not declare any method, commonly known as an empty interface. interface {} is very useful when we need to store any type of value, a bit similar to the C language void * type.

package main

import (
    "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 {
        vals[i] = v
    }
    PrintAll(vals)
}

Note, however, that [] T cannot be directly assigned to [] interface {}

t := []int{1, 2, 3, 4}

var s [] interface {} = t 

The following error is output when compiling:

cannot use t (type [] int) as type [] interface {} in assignment

Finally

I made a brief introduction to the interface features in Go. I think for Go language, the most exquisite design should be the interface. If you are interested, you can study it.

This entry was posted in golang and tagged , , . Bookmark the permalink.

2 Responses to Golang Getting Started Series 4: How to Understand the Interface

  1. good better best says:

    Very good. I like this post very much.

  2. good man says:

    Very Good. I truly appreciate this post.Really thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *