第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解析方法聲明?

如何解析方法聲明?

Go
三國紛爭 2021-09-13 14:55:56
我正在嘗試解析方法聲明?;旧衔倚枰@取接收器基本類型 ( type hello) 和返回類型 ( notypeand error)的語法節(jié)點。這個ast包看起來很簡單,但由于某種原因我沒有得到我需要的數(shù)據(jù)(即字段報告為零)。唯一有用的數(shù)據(jù)似乎只在Object -> Decl該類型的字段中提供,interface{} 所以我認(rèn)為我無法對其進(jìn)行序列化。任何幫助,將不勝感激。代碼如下:package mainimport (    "fmt"    "go/ast"    "go/parser"    "go/token")func main() {    // src is the input for which we want to inspect the AST.    src := `package mypack// type hello is a cool typetype hello string// type notype is not that cooltype notype int// func printme is like nothing else.func (x *hello)printme(s string)(notype, error){    return 0, nil}`    // Create the AST by parsing src.    fset := token.NewFileSet() // positions are relative to fset    f, err := parser.ParseFile(fset, "src.go", src, 0)    if err != nil {        panic(err)    }    // Inspect the AST and find our function    var mf ast.FuncDecl    ast.Inspect(f, func(n ast.Node) bool {        switch x := n.(type) {        case *ast.FuncDecl:            mf = *x        }        return true    })    if mf.Recv != nil {        fmt.Printf("\n receivers:")        for _, v := range mf.Recv.List {            fmt.Printf(",tag %v", v.Tag)            for _, xv := range v.Names {                fmt.Printf("name %v, decl %v, data %v, type %v",                    xv.Name, xv.Obj.Decl, xv.Obj.Data, xv.Obj.Type)            }        }    }}
查看完整描述

1 回答

?
拉丁的傳說

TA貢獻(xiàn)1789條經(jīng)驗 獲得超8個贊

要獲得類型,您需要查看Type可能是 anast.StarExpr或 an的屬性ast.Ident。


在這里看看這個:


package main


import (

    "fmt"

    "go/ast"

    "go/parser"

    "go/token"

)


func main() {

    // src is the input for which we want to inspect the AST.

    src := `

package mypack

// type hello is a cool type

type hello string


// type notype is not that cool

type notype int


// printme is like nothing else.

func (x *hello)printme(s string)(notype, error){

    return 0, nil

}

`

    // Create the AST by parsing src.

    fset := token.NewFileSet() // positions are relative to fset

    f, err := parser.ParseFile(fset, "src.go", src, 0)

    if err != nil {

        panic(err)

    }


    // Inspect the AST and find our function

    var mf ast.FuncDecl

    ast.Inspect(f, func(n ast.Node) bool {

        switch x := n.(type) {

        case *ast.FuncDecl:

            mf = *x

        }

        return true

    })


    if mf.Recv != nil {

        for _, v := range mf.Recv.List {

            fmt.Print("recv type : ")

            switch xv := v.Type.(type) {

            case *ast.StarExpr:

                if si, ok := xv.X.(*ast.Ident); ok {

                    fmt.Println(si.Name)

                }

            case *ast.Ident:

                fmt.Println(xv.Name)

            }

        }

    }

}



查看完整回答
反對 回復(fù) 2021-09-13
  • 1 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號