为什么这些Go类型不同?

如何解决为什么这些Go类型不同?

在下面的示例中,根据time,从GetTime()返回的time.Time类型与types.Identical()不同。为什么是这样?两种类型在打印时均为time.Time

package main

import (
    "fmt"
    "go/ast"
    "go/importer"
    "go/parser"
    "go/token"
    "go/types"

    "golang.org/x/tools/go/packages"
)

const example = `package main

import "time"

func GetTime() time.Time {
    return time.Time{}
}

func main() {
}`

func GetTimeObject() types.Object {
    cfg := &packages.Config{
        Mode:       packages.LoadAllSyntax,}
    loadedPackages,err := packages.Load(cfg,"time")
    if err != nil {
        panic(err)
    }

    timePkg := loadedPackages[0]
    timeObject := timePkg.Types.Scope().Lookup("Time")
    return timeObject
}

func main() {
    fset := token.NewFileSet()

    f,err := parser.ParseFile(fset,"example.go",example,0)
    if err != nil {
        panic(err)
    }

    conf := types.Config{Importer: importer.Default()}
    pkg,err := conf.Check("example",fset,[]*ast.File{f},nil)
    if err != nil {
        panic(err)
    }

    getTimeObj := pkg.Scope().Lookup("GetTime")
    if getTimeObj == nil {
        panic("not found")
    }

    switch t := getTimeObj.Type().(type) {
    case *types.Signature:
        results := t.Results()
        if results.Len() != 1 {
            panic("unexpected")
        }

        retObject := results.At(0)
        timeObject := GetTimeObject()

        fmt.Printf("time type: %v\n",timeObject.Type()) // time.Time
        fmt.Printf("return type: %v\n",retObject.Type()) // time.Time
        fmt.Printf("identical: %t\n",types.Identical(timeObject.Type(),retObject.Type())) // false
    default:
        panic("unexpected")
    }
}

解决方法

它们是不同的,使用%T%#v将代码更改为打印详细信息类型信息,您将有所不同:

        fmt.Printf("time type: %T %#v\n",timeObject,timeObject.Type())                    
        fmt.Printf("return type: %T %#v\n",retObject,retObject.Type())                    

输出:

time type: *types.TypeName &types.Named{info:0x2,obj:(*types.TypeName)(0xc0017e9a90),orig:(*types.Struct)(0xc001b22780),underlying:(*types.Struct)(0xc001b22780),methods:[]*types.Func{(*types.Func)(0xc0017e8a00),(*types.Func)(0xc0017e8a50),(*types.Func)(0xc0017e8aa0),(*types.Func)(0xc0017e9ae0),(*types.Func)(0xc0017e9b30),(*types.Func)(0xc0017e9b80),(*types.Func)(0xc0017e9bd0),(*types.Func)(0xc0017e9c20),(*types.Func)(0xc0017e9c70),(*types.Func)(0xc0017e9cc0),(*types.Func)(0xc0017e9d10),(*types.Func)(0xc0017e9d60),(*types.Func)(0xc0017e9db0),(*types.Func)(0xc0017e9e00),(*types.Func)(0xc0017e9f90),(*types.Func)(0xc00195c000),(*types.Func)(0xc00195c050),(*types.Func)(0xc00195c0a0),(*types.Func)(0xc00195c0f0),(*types.Func)(0xc00195c140),(*types.Func)(0xc00195c190),(*types.Func)(0xc00195c1e0),(*types.Func)(0xc00195c280),(*types.Func)(0xc00195c2d0),(*types.Func)(0xc00195c370),(*types.Func)(0xc00195c3c0),(*types.Func)(0xc00195c410),(*types.Func)(0xc00195c460),(*types.Func)(0xc00195c4b0),(*types.Func)(0xc00195c910),(*types.Func)(0xc00195c960),(*types.Func)(0xc00195ca50),(*types.Func)(0xc00195caa0),(*types.Func)(0xc00195cdc0),(*types.Func)(0xc00195ce10),(*types.Func)(0xc00195ce60),(*types.Func)(0xc00195ceb0),(*types.Func)(0xc00195cf00),(*types.Func)(0xc00195cf50),(*types.Func)(0xc00195cfa0),(*types.Func)(0xc00195cff0),(*types.Func)(0xc00195d040),(*types.Func)(0xc00195d090),(*types.Func)(0xc00195d0e0),(*types.Func)(0xc00195d130),(*types.Func)(0xc00195d180),(*types.Func)(0xc00195d1d0),(*types.Func)(0xc00195d220),(*types.Func)(0xc00195d3b0),(*types.Func)(0xc00195d400)}}
return type: *types.Var &types.Named{info:0x0,obj:(*types.TypeName)(0xc00005ff90),orig:types.Type(nil),underlying:(*types.Struct)(0xc000063320),methods:[]*types.Func{(*types.Func)(0xc000172ff0),(*types.Func)(0xc000173130),(*types.Func)(0xc0001732c0),(*types.Func)(0xc0001733b0),(*types.Func)(0xc0001734a0),(*types.Func)(0xc000173590),(*types.Func)(0xc000173680),(*types.Func)(0xc000173770),(*types.Func)(0xc000173810),(*types.Func)(0xc000173900),(*types.Func)(0xc0001739f0),(*types.Func)(0xc000173b30),(*types.Func)(0xc000173c70),(*types.Func)(0xc000173db0),(*types.Func)(0xc000173ea0),(*types.Func)(0xc000173f90),(*types.Func)(0xc0003e0140),(*types.Func)(0xc0003e0460),(*types.Func)(0xc0003e0550),(*types.Func)(0xc0003e0640),(*types.Func)(0xc0003e0730),(*types.Func)(0xc0003e09b0),(*types.Func)(0xc0003e0af0),(*types.Func)(0xc0003e0c80),(*types.Func)(0xc0003e0d70),(*types.Func)(0xc0003e0e60),(*types.Func)(0xc0003e0f50),(*types.Func)(0xc0003e1040),(*types.Func)(0xc0003e1130),(*types.Func)(0xc0003e1270),(*types.Func)(0xc0003e13b0),(*types.Func)(0xc0003e1590),(*types.Func)(0xc0003e17c0),(*types.Func)(0xc0003e18b0),(*types.Func)(0xc0003e19a0),(*types.Func)(0xc0003e1ae0),(*types.Func)(0xc0003e1bd0),(*types.Func)(0xc0003e1d10),(*types.Func)(0xc0003e1e00),(*types.Func)(0xc0003e1ef0),(*types.Func)(0xc0003e4050),(*types.Func)(0xc0003e4190),(*types.Func)(0xc0003e42d0),(*types.Func)(0xc0003e4410),(*types.Func)(0xc0003e4550),(*types.Func)(0xc0003e4690),(*types.Func)(0xc0003e47d0),(*types.Func)(0xc0003e4910),(*types.Func)(0xc0003e4a50),(*types.Func)(0xc0003e4b90)}}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?