美文网首页
探究golang的linkname

探究golang的linkname

作者: wu_sphinx | 来源:发表于2018-10-27 21:58 被阅读111次

在编写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.Sleeptime包里,是可导出,而timeSleepruntime包里面,是不可导出了,那么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

    在编写golang程序的过程中,会经常有一些sleep的需求,于是我们使用time.Sleep函数跳转到函数定义处...

  • 探究golang接口

    今天看了《Go 语言中的方法,接口和嵌入类型》所以想对比以前对C/C++相关知识进一步理解golang的接口、指针...

  • golang iris使用go:linkname添加自定义for

    在使用iris的时候我们有这么一个结构体 我们有一个start_time的字段,如果前端希望传入2019-08-0...

  • go-linkname

    假设有一个内部包,它提供一个方法如下: 这个方法是内部使用的,它没有导出属性,因此它无法被其他外部包import,...

  • go modules的replace使用, 解决fork的项目i

    探究问题所在 如果直接第三方依赖, golang是可以满足使用的. 但如果我们需要修复第三方依赖的bug, 抑或为...

  • 一起用golang之Go程序的套路

    系统性地介绍golang基础的资料实在太多了,这里不再一一赘述。本文的思路是从另一个角度来由浅入深地探究下Go程序...

  • Golang 反射实现依赖注入

    Golang 反射实现依赖注入 Coding/Golang #Golang #Golang/reflect 依赖注...

  • golang学习资源

    • golang官方文档• golang官方指南• golang官方指南目录版• 怎样编写golang代码• 地道...

  • 安装 golang 最简易教程

    1. 安装golang 到golang.org官网下载最新版的golang, Leanote至少需要golang ...

  • 01 mac-go开发环境搭建

    安装golang安装包 Golang 官网 : https://golang.org/Golang 中文网 htt...

网友评论

      本文标题:探究golang的linkname

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