SoFunction
Updated on 2025-03-05

Basic usage tutorial for struct and interface in golang

Preface

This article mainly introduces to you about struct and interface in golang. It is the basic knowledge of golang. I won’t say much below. Let’s take a look at the detailed introduction together.

struct

struct Use custom complex data structures to contain multiple fields (attributes) and can be nested; the struct type in go is understood as a class, which can define methods, and has some differences between definitions of functions; 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)

struct method

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

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

The above code declares a method named Name for the custom type player, which returns a string. It is worth noting that (p *player) code specifies that we create a method for the player and pass the instance pointer that calls the method into the function as a variable p. If there is no (p *player) code, this method becomes an ordinary global function.

Embedding of struct (Embedding)

There is a big difference between "inheritance" in the go language and inheritance in other languages, such as:

type player struct{
 User
}

This is a way of writing "inheritance". In the go language, this method is called "embed". At this time, the player type has variables such as User type Name and other variables

struct tag

This method is mainly used to convert xml, json and struct, which is very convenient and intuitive. For example, the parameters given by the interface are usually transmitted from json, but we need to convert them to struct and then process them.

example:

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

interface

golang does not support the complete object-oriented idea, it has no inheritance, and polymorphism depends entirely on interface implementation. golang can only simulate inheritance, its essence is combination, but the golang language provides us with some syntactic sugar to make it look like it has achieved the effect of inheritance. The interface in Golang does not require an implementation of display. The Interface type can define a set of methods, but these do not require implementation. And the interface cannot contain any variables. As long as one variable contains all methods in the interface type, this variable implements this interface. Therefore, there is no implement-like keyword in golang; if a variable contains multiple methods of one interface type, then this variable implements multiple interfaces; if a variable contains only one interface part of the method, then this variable does not implement this interface.

Definition of interface

The interface type is a pointer by default.

example:

Interface definition

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

Empty interface Interface{}: Empty interface has no method, so all types implement empty interfaces.

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

Polymorphism of interface

Various forms of a thing can be operated according to a unified interface. This method is used the most, a bit 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 Nesting

One interface can be nested in another interface. That is, a method that requires two interfaces to be implemented. In the following example Used contains all methods of the Car interface.
example:

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

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.