source

package module
v0.11.3 Latest Latest
Warning

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

Go to latest
Published: May 18, 2024 License: MIT Imports: 15 Imported by: 14

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, ignoreRuntime bool, msg ...any)

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

skip 需要忽略的内容。

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

ignoreRuntime 表示是否不显示 runtime 下的系统调用信息; 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 PkgPath added in v0.10.0

func PkgPath(p string) (string, error)

PkgPath 文件或目录 p 的导出路径

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

func PkgSourceDir added in v0.11.0

func PkgSourceDir(pkgPath, modDir string, replace bool) (dir string, err error)

PkgSourceDir 查找包 pkgPath 的源码目录

如果 pkgPath 是标准库的名称,如 encoding/json 等,则返回当前使用的 Go 版本对应的标准库地址。 其它情况则从 modDir 指向的 go.mod 中查找 require 或是 replace 字段的定义, 并根据这些定义找到其指向的源码路径。

pkgPath 需要查找的包路径,如果指向的是模块下的包级别的导出路径,则会尝试使用 strings.HasPrefix 与 require 指令进行对比; modDir go.mod 所在的目录,将在该文件中查找 pkgPath 指定的目录; replace 是否考虑 go.mod 中的 replace 指令的影响;

NOTE: 这并不会检测 dir 指向目录是否真实且准确。

func Stack added in v0.2.0

func Stack(skip int, ignoreRuntime bool, 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