dongtangyi8962 2013-11-27 05:07
浏览 47
已采纳

获取结构字段类型的简单字符串表示形式

Using Go’s ast package, I am looping over a struct’s field list like so:

type Thing struct {
    Field1 string
    Field2 []int
    Field3 map[byte]float64
}

// typ is a *ast.StructType representing the above   
for _, fld := range typ.Fields.List {
    // get fld.Type as string
}

…and would like to get a simple string representation of fld.Type, as it appears in the source code, eg ”[]int” or “map[byte]float64”.

The ast package field type Type property is an Expr, so I’ve found myself getting off into the weeds using type switches and handling every type specifically – when my only goal is to get out the plain string to the right of each field name, which seems like it should be simpler.

Is there a simple way?

  • 写回答

3条回答 默认 最新

  • doukai2839 2013-11-27 06:06
    关注

    There are two things you could be getting at here, one is the type of an expression as would ultimately be resolved during compilation and the other is the code which would determine that type.

    Digging through the docs, I don't believe the first is at all available. You can get at the later, however, by using End() and Pos() on Node.

    Quick example program:

    package main
    
    import (
        "fmt"
        "go/ast"
        "go/parser"
        "go/token"
    )
    
    func main() {
        src := `
            package foo
    
        type Thing struct {
        Field1 string
        Field2 []int
        Field3 map[byte]float64
      }`
    
        fset := token.NewFileSet()
        f, err := parser.ParseFile(fset, "", src, 0)
    
        if err != nil {
            panic(err)
        }
    
        // hard coding looking these up
        typeDecl := f.Decls[0].(*ast.GenDecl)
        structDecl := typeDecl.Specs[0].(*ast.TypeSpec).Type.(*ast.StructType)
        fields := structDecl.Fields.List
    
        for _, field := range fields {
            typeExpr := field.Type
    
            start := typeExpr.Pos() - 1
            end := typeExpr.End() - 1
    
            // grab it in source
            typeInSource := src[start:end]
    
            fmt.Println(typeInSource)
        }
    
    }
    

    This prints:

    string
    []int
    map[byte]float64
    

    I through this together in the golang playground, if you want to mess with it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程