在调试Golang代码时,在${GOPATH}/src/runtime/proc.go
中看到
//go:linkname main_main main.main
func main_main()
于是乎,//go:linkname
到底是什么?
这里确实没有比较好的搜索入口,不知道大家是怎么检索一些Golang Inside Feature的用法说明?欢迎在评论区交流
通过查阅相关信息,在标准库的文档说明中找到
//go:linkname localname [importpath.name]
The //go:linkname directive conventionally precedes the var or func declaration named by “localname“, though its position does not change its effect. This directive determines the object-file symbol used for a Go var or func declaration, allowing two Go symbols to alias the same object-file symbol, thereby enabling one package to access a symbol in another package even when this would violate the usual encapsulation of unexported declarations, or even type safety. For that reason, it is only enabled in files that have imported "unsafe".
但不推荐使用,这种行为是破坏Go Module的依赖关系,会使得包的独立性丧失。
更多相关材料:
网友评论