Understand the three laws of go language reflection and unlock a new realm of programming

王林
Release: 2024-04-07 13:39:01
Original
578 people have browsed it

Three laws of Go language reflection: Separation of types and values: Types describe data structures, and values ​​represent specific instances. Data values ​​are operable: reflection allows access and modification of values, implementing dynamic data structures and other functions. Used through the reflect package: Reflection operations are implemented through the reflect package, which provides tools for inspecting and manipulating values, types, and interfaces.

Understand the three laws of go language reflection and unlock a new realm of programming

Understand the three laws of Go language reflection

In the Go language, reflection allows the program to inspect and operate its own data structure at runtime. By understanding and applying the three laws, you can take full advantage of the power of reflection and achieve flexible, dynamic programming.

Law 1: Type and value are not the same thing

Type describes the structure of data, while value represents a specific instance of data. Reflection allows you to obtain type information about a value without knowing its specific type beforehand.

Code example:

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{Name: "John", Age: 30}
    t := reflect.TypeOf(p) // 获得值类型
    fmt.Println(t) // 输出: main.Person
}
Copy after login

Law 2: Values ​​can be manipulated

Reflection is not limited to obtaining type information; Allows you to access and modify the value itself. This means you can use reflection to implement dynamic data structures, custom serializers, and more.

Code example:

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{Name: "John", Age: 30}
    v := reflect.ValueOf(p) // 获得值
    v.FieldByName("Age").SetInt(31) // 修改值
    fmt.Println(p) // 输出: {John 31}
}
Copy after login

Law 3: Use the reflect package

All reflection operations are done through reflect Implemented by package. This package provides a set of types and functions that enable you to inspect and manipulate values, types, and interfaces.

Practical case:

Imagine you have a list of unknown structured data from a database. You can use reflection to traverse the list and dynamically obtain the type and data to which each value belongs:

type Person struct {
    Name string
    Age  int
}

type Address struct {
    Street  string
    City    string
    Country string
}

func main() {
    data := []interface{}{
        Person{Name: "John", Age: 30},
        Address{Street: "Main St.", City: "Anytown", Country: "USA"},
    }

    for _, v := range data {
        t := reflect.TypeOf(v)
        fmt.Println("Type:", t.Name())

        v = reflect.ValueOf(v)
        for i := 0; i < v.NumField(); i++ {
            field := v.Field(i)
            fmt.Println("  Field:", t.Field(i).Name, " Value:", field.Interface())
        }
    }
}
Copy after login

The above is the detailed content of Understand the three laws of go language reflection and unlock a new realm of programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!