-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkg.go
More file actions
35 lines (32 loc) · 1.29 KB
/
Copy pathpkg.go
File metadata and controls
35 lines (32 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package sure
import (
"reflect"
"strings"
)
// GetPkgPath returns the package path of the sure package
// Uses reflection to get the path from ErrorHandlingMode type
// Returns the absolute import path as a string
//
// GetPkgPath 返回 sure 包的包路径
// 使用反射从 ErrorHandlingMode 类型获取路径
// 返回绝对导入路径作为字符串
func GetPkgPath() string {
return reflect.TypeFor[ErrorHandlingMode]().PkgPath()
}
// GetPkgName returns the package name of the sure package
// Reads it from the reflected type name (e.g. "sure.ErrorHandlingMode"), which
// stays correct even on versioned module paths whose trailing path segment is
// "v2" instead of the genuine package name
//
// GetPkgName 返回 sure 包的包名
// 从反射的类型名(形如 "sure.ErrorHandlingMode")取包名段;
// 即便是 /vN 版本化模块(路径末段是 "v2" 而非真实包名)也依然正确
func GetPkgName() string {
// 勿"简化"成 syntaxgo_reflect.GetPkgNameV2:它内部是 path.Base(pkgPath),
// 对 /vN 版本化模块会返回末段 "v2" 而非真实包名。这里从类型名取包名段才是 /vN 安全的写法。
typeName := reflect.TypeFor[ErrorHandlingMode]().String()
if packageName, _, ok := strings.Cut(typeName, "."); ok {
return packageName
}
return typeName
}