美文网首页
Golang编译指令//go:linkname

Golang编译指令//go:linkname

作者: 牛奶言兼 | 来源:发表于2024-09-04 20:55 被阅读0次

在调试Golang代码时,在${GOPATH}/src/runtime/proc.go 中看到

//go:linkname main_main main.main
func main_main()

于是乎,//go:linkname 到底是什么?

这里确实没有比较好的搜索入口,不知道大家是怎么检索一些Golang Inside Feature的用法说明?欢迎在评论区交流

通过查阅相关信息,在标准库的文档说明中找到

compile command - cmd/compile - Go Packages

//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的依赖关系,会使得包的独立性丧失。

更多相关材料:

相关文章

网友评论

      本文标题:Golang编译指令//go:linkname

      本文链接:https://www.haomeiwen.com/subject/arhrljtx.html