duanluanhui8348 2016-01-29 01:18
浏览 30
已采纳

反映:是否有可能获取基础类型类型信息?

I'm porting a program from go/ast to reflect. In order to pass the tests I need to get not only the top type information but also the underlying type if the underlying type is not built-in.

In the example below, is it possible for a program to know that the underlying type of main.T is main.TT?

package main

import "fmt"
import "reflect"

func main() {
    type TT int
    type T TT

    x := T(0)
    fmt.Println(reflect.TypeOf(x))
}

Output:

 main.T
  • 写回答

1条回答 默认 最新

  • duanrang9348 2016-01-29 02:46
    关注

    The underlying type of main.T is int, not main.TT. The reflect package has no knowledge that main.T was declared with main.TT.

    Here's what the specification has to say about underlying types:

    Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself. Otherwise, T's underlying type is the underlying type of the type to which T refers in its type declaration.

    type T1 string
    type T2 T1
    type T3 []T1
    type T4 T3
    

    The underlying type of string, T1, and T2 is string. The underlying type of []T1, T3, and T4 is []T1.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大