How to get type information in go language

青灯夜游
Release: 2023-02-22 19:14:13
Original
4664 people have browsed it

In the go language, type information can be obtained through reflection; you only need to call the TypeOf() function of the reflect package. Method: 1. Use the "reflect.TypeOf()" function to obtain the type object of the specified variable; 2. Use Name() and Kind() to obtain the type information of the type object. The syntax is "type object.Name()" and "type object .Kind()".

How to get type information in go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

In the go language, type information can be obtained through reflection.

In the Go language, by calling the reflect.TypeOf function, we can create a reflect.Type value from a value of any non-interface type. The reflect.Type value represents the type of this non-interface value. Through this value, we can get a lot of information about this non-interface type. Of course, we can also pass an interface value to a reflect.TypeOf function call, but this call will return a reflect.Type value that represents the dynamic type of the interface value.

In fact, the type of the only parameter of the reflect.TypeOf function is interface{}, and the reflect.TypeOf function will always return a reflect.Type value that represents the dynamic type of this unique interface parameter value.

How to get a reflect.Type value that represents a certain interface type? We must achieve this goal through some indirect ways that will be introduced below.

Type reflect.Type is an interface type, which specifies several methods. Through these methods, we can observe various information of the Go type represented by a reflect.Type value. Some of these methods apply to all kinds of types, and some only apply to one or a few types. Calling a method with an inappropriate reflect.Type owner value will cause a panic at runtime.

Use the reflect.TypeOf() function to obtain a type object (reflect.Type) of any value. The program can access the type information of any value through the type object. Let’s use an example to understand the process of obtaining type objects:

package main
import (
    "fmt"
    "reflect"
)
func main() {
    var a int
    typeOfA := reflect.TypeOf(a)
    fmt.Println(typeOfA.Name(), typeOfA.Kind())
}
Copy after login

The code output is as follows:

How to get type information in go language

The code description is as follows:

  • Line 10 defines a variable of type int.

  • In line 12, the type object typeOfA of variable a is obtained through reflect.TypeOf(), and the type is reflect.Type().

  • In line 14, through the member function of the typeOfA type object, the type name of the typeOfA variable can be obtained respectively int, and the type (Kind) is int.

Extended knowledge: Type and Kind of reflection

When using reflection, you need to first understand the type (Type) and the difference between Kind. In programming, the most commonly used type is type, but in reflection, when a large variety of types needs to be distinguished, kind (Kind) is used. For example, when you need to uniformly judge pointers in types, it is more convenient to use kind (Kind) information.

The above is the detailed content of How to get type information in go language. 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