source

package module
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 12 Imported by: 13

README

source Go license codecov Go version PkgGoDev

source 模块提供了一些与源码相关的功能

  • DumpGoSource 输出并格式化 Go 的源代码;
  • CurrentFile 相当于部分语言的 __FILE__
  • CurrentDir 相当于部分语言的 __DIR__
  • CurrentLine 相当于部分语言的 __LINE__
  • CurrentFunction 相当于部分语言的 __FUNCTION__
  • Stack 返回调用者的堆栈信息;
  • ModFile 文件或目录 p 所在模块的 go.mod 内容;
  • ModDir 向上查找 p 所在的目录的 go.mod;
  • PackagePath 文件或目录 p 所在 Go 文件的导出路径;

安装

go get github.com/issue9/source

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package source 提供与 Go 源码相关的一些操作

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CurrentDir

func CurrentDir() string

CurrentDir 获取`调用者`所在的目录

相当于部分语言的 __DIR__

func CurrentFile

func CurrentFile() string

CurrentFile 获取`调用者`所在的文件

相当于部分语言的 __FILE__

func CurrentFunction

func CurrentFunction() string

CurrentFunction 获取`调用者`所在的函数名

相当于部分语言的 __FUNCTION__

func CurrentLine

func CurrentLine() int

CurrentLine 获取`调用者`所在的行

相当于部分语言的 __LINE__

func CurrentLocation added in v0.1.2

func CurrentLocation() (path string, line int)

CurrentLocation 获取`调用者`当前的位置信息

func CurrentPath

func CurrentPath(path string) string

CurrentPath 获取`调用者`所在目录的路径

类似于部分语言的的 __DIR__ + "/" + path

func DumpStack added in v0.3.1

func DumpStack(w io.Writer, skip int, msg ...any)

DumpStack 将调用的堆栈信息写入 w

skip 需要忽略的内容。

  • 1 表示 Stack 自身;
  • 2 表示 Stack 的调用者,以此类推;

msg 表示需要输出的额外信息;

func ModDir added in v0.6.0

func ModDir(p string) (string, error)

ModDir 向上查找 p 所在的目录的 go.mod

func ModFile added in v0.4.0

func ModFile(p string) (string, *modfile.File, error)

ModFile 文件或目录 p 所在模块的 go.mod 内容

从当前目录开始依次向上查找 go.mod,从其中获取 go.mod 文件位置,以及文件内容的解析。

func PackagePath added in v0.8.0

func PackagePath(p string) (string, error)

PackagePath 文件或目录 p 所在 Go 文件的导出路径

会向上查找 go.mod,根据 go.mod 中的 module 结合当前目录组成当前目录的导出路径。

func Stack added in v0.2.0

func Stack(skip int, msg ...any) string

Stack 返回调用堆栈信息

skip 需要忽略的内容。

  • 1 表示 Stack 自身;
  • 2 表示 Stack 的调用者,以此类推;

msg 表示需要输出的额外信息;

Types

This section is empty.

Directories

Path Synopsis
Package codegen 简单的代码生成工具
Package codegen 简单的代码生成工具

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL