首页 > 其他分享 >golang ast获取注释信息

golang ast获取注释信息

时间:2022-12-13 15:02:26浏览次数:37  
标签:node 注释 fset ast demo golang func go

 

package main

import (
	"go/ast"
	"go/parser"
	"go/token"
	"log"
	"path/filepath"
)

type Visitor struct {
	fset *token.FileSet
}

func (v *Visitor) Visit(node ast.Node) ast.Visitor {
	switch node.(type) {
	//判断ast分类
	case *ast.FuncDecl:
		demo := node.(*ast.FuncDecl)

		// 打印具体的注释
		println(demo.Doc.List[0].Text)

		// 可以打印出ast结构
		ast.Print(v.fset, node)
	}

	return v
}

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

	path, _ := filepath.Abs("./demo.go")
	f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
	if err != nil {
		log.Println(err)
		return
	}

	ast.Walk(&Visitor{
		fset: fset,
	}, f)

}

  

demo.go 被分析文件

// file commment
package main

type Article struct{}

// @GET("/get")
func (a Article) Get() {

}

// @POST("/save")
func (a Article) Save() {

}

 输出:

// @GET("/get")
// @POST("/save")

 

标签:node,注释,fset,ast,demo,golang,func,go
From: https://www.cnblogs.com/itsuibi/p/16978790.html

相关文章

  • ThreadLocal(2) - FastThreadLocal
    FastThreadLocal该类位于netty的util包下,netty的线程都使用的是FastThreadLocal而不是jdk的ThreadLocaljdk的ThreadLocal使用Thread类的ThreadLocalMapnetty的Fas......
  • 000 通过 Pytorch 实现 Transformer 框架完整代码(带注释)
    #!/usr/bin/python3.9#-*-coding:utf-8-*-#@Time:2021/10/2910:48#@Author:nickchen121#@File:abd_transformer_cyd.py#@Software:PyCharmimportma......
  • golang windows endless 运行报错 undefined: syscall.SIGUSR1
    为了gin服务的热更新,采用了 endless+fresh的方案,安装endless后无法在windows本地调试,采用以下解决方案就好了解决地址:​​golangwindows运行报错undefined:syscall.SI......
  • ElasticSearch原理篇
    一、开篇几个问题 1、大规模数据如何检索?当系统数据量上了10亿、100亿条的时候,我们在做系统架构的时候通常会从以下角度去考虑:1)用什么数据库好?(MySQL、sybase、Oracle、达......
  • 【开源系统脚手架】人人快速开发框架 人人VUE(renren-fast-vue)启动教程
     代码​​https://www.renren.io/guide/#project​​​​https://github.com/renrenio/renren-fast-vue​​ 1.nodejs需使用8.0版本2.更改策略,设置权限(管理员打开cmd)......
  • 【安装】Linux安装Elasticsearch教程
    Elastic官网​​开源搜索:Elasticsearch、ELKStack和Kibana的开发者|Elastic​​Elasticsearch(官网:​​https://www.elastic.co/cn/products/elasticsearch​​ )需要......
  • 谷歌浏览器安装elasticsearch-head插件
    之前在使用es的时候有插入索引和数据的需求但是对于es操作不适太熟悉,然后就用docker安装了一个es-head进行操作,但是用docker安装的es-head有时候不是太好用(问题后续标注)。......
  • golang+gin+download
    //main.gopackagemainimport( "fmt" "log" "time" "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin")funcuseGzip(engine*gin.Engine){ eng......
  • Elasticsearch Head插件使用小结
    作者:崔雄华1ElasticsearchHead是什么ElasticSearchhead就是一款能连接ElasticSearch搜索引擎,并提供可视化的操作页面对ElasticSearch搜索引擎进行各种设置和数据检索......
  • golang file
    O_RDONLY:只读模式打开文件;O_WRONLY:只写模式打开文件;O_RDWR:读写模式打开文件;O_APPEND:写操作时将数据附加到文件尾部(追加);O_CREATE:如果不存在将创建一个新文件;O_EXCL:和......