SoFunction
Updated on 2025-03-05

Share advanced skills of structures in Go language

Combination structure

Structures in Go language can also be combined to achieve the function of reuse. The specific implementation method is as follows:

type Person struct {
    Name string
    Age  int
}
type Student struct {
    P      Person // Combined Person structure    School string
}
func main() {
    s := Student{Person{"Tom", 18}, "High School"}
    (, , ) // Output Tom 18 High School}

This code defines a Person structure, which contains two fields Name and Age, and also defines a Student structure, which combines Person structure. In the main function, we create a Student instance s through the constructor and access its Name, Age and School fields. We need to access the fields in the Person structure.

By combining other structures, a child structure can replicate fields and methods in the parent structure, but it does not inherit the characteristics and behavior of the parent structure. Compared with inheritance, the combination is more flexible and simple, but it also requires more manual operations from developers.

It should be noted that there is no concept of class in Go language, so structures are not classes. They cannot implement object-oriented features such as inheritance and polymorphism, but are a way to define data types.

Nested structure

Another structure can be nested in the structure to form a composite structure type. A nested structure can directly use the fields of the internal structure, or you can access the fields of the internal structure through the internal structure name.

For example:

type Address struct {
    Province string
    City     string
}
type Person struct {
    Name    string
    Age     int
    Address Address
}
p := Person{Name: "Tom", Age: 18, Address: Address{Province: "Beijing", City: "Chaoyang"}}
(, , , )

Pointer structure

Structural variables usually occupy a relatively large memory space. In order to save memory, you can use pointer structures. The pointer structure can obtain the address of the structure variable through the & operator and access the value of the structure variable through the * operator.

For example:

type Person struct {
    Name string
    Age  int
}
p := &Person{Name: "Tom", Age: 18}
((*p).Name, (*p).Age) // Output: Tom 18(, )       // Output:Tom 18

Anonymous field

One structure type can be used as a field of another structure type, and this field is called an anonymous field. The type of an anonymous field can be any type, it can be a basic type, a structure type, a pointer type, etc. An anonymous field can directly access the fields of the internal structure, or the fields of the internal structure can be accessed through the internal structure name.

For example:

type Address struct {
    Province string
    City     string
}
type Person struct {
    Name string
    Age  int
    Address
}
p := Person{Name: "Tom", Age: 18, Address: Address{Province: "Beijing", City: "Chaoyang"}}
(, , , )

Object-oriented

Although Go is not a purely object-oriented programming language, it provides some basic features of object-oriented programming, such as encapsulation, inheritance, and polymorphism.

Package

Go language implements encapsulation through fields or methods starting with capital letters that can be accessed outside the package. Fields or methods beginning with lowercase letters can only be accessed inside the current package.

For example:

package main
import "fmt"
type Person struct {
    name string
    age int
}
func (p *Person) SayHello() {
    ("Hello, my name is", , ", I'm", , "years old.")
}
func main() {
    p := &Person{"Tom", 18}
    ()
}

In the above code, we define a structure called Person, which has two fields name and age, and defines a method SayHello, which is used to print personal information. In the main function, we create a Person object p, and call its SayHello method, outputting its information.

inherit

The Go language does not support traditional inheritance methods, but inheritance can be achieved through structure combinations. Specifically, we can include another structure in one structure and inherit its fields and methods by nesting structures.

For example:

package main
import "fmt"
type Person struct {
    name string
    age  int
}
func (p *Person) SayHello() {
    ("Hello, my name is", , ", I'm", , "years old.")
}
type Student struct {
    Person
    score int
}
func main() {
    s := &Student{Person{"Tom", 18}, 90}
    ()
    ("My score is", )
}

In the above code, we define two structures Person and Student. The Student structure contains a Person-type field and defines its own score field. In the main function, we create a Student object s, call its SayHello method, output its information, and then output its results.

Polymorphic

Go does not support the traditional polymorphic method, but it can be implemented through interfaces. Specifically, we can define an interface and let different structures implement this interface, thereby achieving polymorphism.

For example:

package main
import "fmt"
type Animal interface {
    Say()
}
type Cat struct{}
func (c *Cat) Say() {
    ("Meow!")
}
type Dog struct{}
func (d *Dog) Say() {
    ("Woof!")
}
func main() {
    var a Animal
    a = &Cat{}
    ()
    a = &Dog{}
    ()
}

This Go code defines an Animal interface and two structures that implement the interface: Cat and Dog. There is only one Say() method in the Animal interface. Cat and Dog implement the Say() method and output the corresponding sound respectively. In the main function, an a variable is defined, with the type Animal interface. First assign a to a pointer to a Cat structure, and then call a Say() method to output "Meow!". Then assign a to a pointer to a Dog structure, and call a Say() method again to output "Woof!".

This is the end of this article about advanced skills in structures in Go. For more relevant Go structure content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!