Basic usage tutorials for struct and interface in golang

  • 2020-06-12 09:30:16
  • OfStack

preface

This article mainly introduces the content of struct and interface in golang, which is the basic knowledge of golang. Let's start with a detailed introduction.

struct

struct USES custom complex data structures that can contain multiple fields (properties) and can be nested. The struct type in go is understood as a class that defines methods, slightly different from function definitions; The struct type is a value type.

struct definition


type User struct {
 Name string
 Age int32
 mess string
}
var user User
var user1 *User = &User{}
var user2 *User = new(User)

The method of struct

In go, we can define type-specific methods for custom types, such as:


func (p *player) Name() string{
 return p.name
}

The above code declares a method named Name for the custom type player, which returns 1 string. It is worth noting that (p *player) this code specifies that we are creating a method for player and passing in the instance pointer calling the method as a variable p. Without (p *player) this code becomes a normal global function.

struct embedding (Embedding)

Inheritance in go is very different from inheritance in other languages, such as:


type player struct{
 User
}

This is a form of "inheritance" called "embedding" (embed) in go, where the player type has variables such as Name of the User type

The struct tag

This method is mainly used to convert xml, json and struct to each other, which is very convenient and intuitive. For example, the parameter 1 given by the interface is generally json, but internally we need to convert to struct for processing.

Example:


import "encoding/json"
type User struct {
 Name string `json:"userName"`
 Age int `json:"userAge"`
}
func main() {
 var user User
 user.Name = "nick"
 user.Age = 18 
 conJson, _ := json.Marshal(user)
 fmt.Println(string(conJson)) //{"userName":"nick","userAge":0}
}

interface

golang does not support full object orientation, it does not inherit, and polymorphism is completely dependent on the interface implementation. golang only simulates inheritance, which is essentially composition, but golang gives us a little syntax sugar to make it look like inheritance. The interface in Golang does not need to be displayed by the implementation. The Interface type can define a set of methods, but these do not need to be implemented. And interface cannot contain any variables. As long as a variable contains all the methods in the interface type, that variable implements the interface. Therefore, there is no implement similar keyword in golang; If a variable contains multiple methods of type interface, then the variable implements multiple interfaces. If a variable contains only 1 square method of interface, then the variable does not implement this interface.

The definition of interface

The interface type defaults to 1 pointer.

Example:

Interface definition


type Car interface {
 NameGet() string
 Run(n int)
 Stop()
}

Empty interface Interface{} : Empty interfaces have no methods, so all types implement empty interfaces.


var a int
var b interface{} // Empty interface 
b = a

interface polymorphism

Various forms of one kind of thing can be operated according to the interface of Unity 1. This approach is used most often, somewhat like class inheritance in c++.

Example:


type Item interface {
 Name() string
 Price() float64
}
 
type VegBurger struct {
}
 
func (r *VegBurger) Name() string{
 return "vegburger"
}
 
func (r *VegBurger) Price() float64{
 return 1.5
}
 
type ChickenBurger struct {
}
 
func (r *ChickenBurger) Name() string{
 return "chickenburger"
}
 
func (r *ChickenBurger) Price() float64{
 return 5.5
}

Interface nested

1 interface can be nested in another interface. That is, a method that needs to implement two interfaces. In the following example, Used contains all the methods of the INTERFACE Car.
Example:


type Car interface {
 NameGet() string
 Run(n int)
 Stop()
}
type Used interface {
 Car
 Cheap()
}

conclusion


Related articles: