Golang 数据类型

Golang 数据类型大体可分为以下几类:

  • 基本类型:
    • 布尔型 true/false,例如 bool := false
    • 字符串类型:由单个字节链接起来,默认为 UTF-8 Unicode
    • 数字类型
      • 整型 int/int8/int16/int32/int64/unit8/uint16/uint32/uint64/uintptr
        • int, uintuintptr 在 32 位系统上通常为 32 位宽,在 64 位系统上则为 64 位宽。 当你需要一个整数值时应使用 int 类型,除非你有特殊的理由使用固定大小或无符号的整数类型。
        • byte: uint8的别名;rune :int32的别名、表示一个 Unicode 码点;uintptr: 无符号整型,用于存放指针)
      • 浮点型: float32/float64/complex64/complex128
  • 派生类型:指针/数组/函数/struct/channel/slice/interface/map

以上你应该已经很熟悉了,至于更具体的说明,可至官方文档及相关课程。

Golang Reflect

Package reflect implements run-time reflection, allowing a program to manipulate objects with arbitrary types. The typical use is to take a value with static type interface{} and extract its dynamic type information by calling TypeOf, which returns a Type.

A call to ValueOf returns a Value representing the run-time data. Zero takes a Type and returns a Value representing a zero value for that type.

Golang语言实现了反射,反射机制就是在运行时动态的调用对象的方法和属性,官方自带的 reflect 包就是反射相关的,只要包含这个包就可以使用。其中,基本功能就是 TypeOfValueOf,分别可以动态获取输入参数接口中的值类型及数据的值。

P.S Golang 的 gRPC也是通过反射实现的

type Kind

https://pkg.go.dev/reflect#Kind

A Kind represents the specific kind of type that a Type represents. The zero Kind is not a valid kind.

尽管具体的数据类型可以有无限种,但是它们可以被分为几种类型。这个就是reflect.Kind。