内容简介:在编写golang程序的过程中,会经常有一些sleep的需求,于是我们使用跳转到函数定义处发现这个函数定义如下:没错,只有定义没有实现?显然不是,函数的实现在
在编写golang程序的过程中,会经常有一些sleep的需求,于是我们使用 time.Sleep
函数
跳转到函数定义处发现这个函数定义如下:
// Sleep pauses the current goroutine for at least the duration d. // A negative or zero duration causes Sleep to return immediately. func Sleep(d Duration)
没错,只有定义没有实现?显然不是,函数的实现在 runtime/time.go
// timeSleep puts the current goroutine to sleep for at least ns nanoseconds.
//go:linkname timeSleep time.Sleep
func timeSleep(ns int64) {
if ns <= 0 {
return
}
gp := getg()
t := gp.timer
if t == nil {
t = new(timer)
gp.timer = t
}
*t = timer{}
t.when = nanotime() + ns
t.f = goroutineReady
t.arg = gp
tb := t.assignBucket()
lock(&tb.lock)
if !tb.addtimerLocked(t) {
unlock(&tb.lock)
badTimer()
}
goparkunlock(&tb.lock, waitReasonSleep, traceEvGoSleep, 2)
}
看看 go:linkname
的 官方说明
//go:linkname localname importpath.name The //go:linkname directive instructs the compiler to use “importpath.name” as the object file symbol name for the variable or function declared as “localname” in the source code. Because this directive can subvert the type system and package modularity, it is only enabled in files that have imported "unsafe".
这个指令告诉编译器为当前源文件中私有函数或者变量在编译时链接到指定的方法或变量。因为这个指令破坏了类型系统和包的模块化,因此在使用时必须导入 unsafe
包,所以可以看到 runtime/time.go
文件是有导入 unsafe
包的。
我们看到 go:linkname
的格式,这里 localname
自然对应 timeSleep
, importpath.name
就对应 time.Sleep
,但为什么要这么做呢?
我们知道 time.Sleep
在 time
包里,是可导出,而 timeSleep
在 runtime
包里面,是不可导出了,那么 go:linkname
的意义在于让 time
可以调用 runtime
中原本不可导出的函数,有点 hack
,举个栗子:
目录结构如下
➜ demo git:(master) ✗ tree
.
├── linkname
│ └── a.go
├── main.go
└── outer
└── world.go
文件内容 a.go
package linkname
import _ "unsafe"
//go:linkname hello examples/demo/outer.World
func hello() {
println("hello,world!")
}
world.go
package outer
import (
_ "examples/demo/linkname"
)
func World()
main.go
package main
import (
"examples/demo/outer"
)
func main() {
outer.World()
}
运行如下:
# examples/demo/outer outer/world.go:7:6: missing function body
难道理解错了,这是因为 go build
默认加会加上 -complete
参数,这个参数检查到 World()
没有方法体,在 outer
文件夹中增加一个空的 .s
文件即可绕过这个限制
➜ demo git:(master) ✗ tree
.
├── linkname
│ └── a.go
├── main.go
└── outer
├── i.s
└── world.go
输出如下:
hello,world!
参考:
以上所述就是小编给大家介绍的《探究golang的linkname用法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
大连接
[美] 尼古拉斯•克里斯塔基斯(Nicholas A. Christakis)、[美] 詹姆斯•富勒(James H. Fowler) / 简学 / 中国人民大学出版社 / 2013-1 / 59.90元
[内容简介] 1. 本书是继《六度分隔》之后,社会科学领域最重要的作品。作者发现:相距三度之内是强连接,强连接可以引发行为;相聚超过三度是弱连接,弱连接只能传递信息。 2. 本书讲述了社会网络是如何形成的以及对人类现实行为的影响,如对人类的情绪、亲密关系、健康、经济的运行和政治的影响等,并特别指出,三度影响力(即朋友的朋友的朋友也能影响到你)是社会化网络的强连接原则,决定着社会化网络的......一起来看看 《大连接》 这本书的介绍吧!