这里填写标题
1. 突破限制, 访问其它 Go package 中的私有函数
熟悉 C++、Java、C#等面向对象的编程语言的同学, 在学习 Go 语言的过程中, 经常会被访问权限所困扰, 逐渐才能了解这样一个事实:
Go 语言通过 identifier 的首字母是否大写来决定它是否可以被其它 package 所访问。
正式的 Go 语言规范是这么规定的:
An identifier may be exported to permit access to it from another package. An identifier is exported if both:
the first character of the identifier’s name is a Unicode upper case letter (Unicode class “Lu”); and
the identifier is declared in the package block or it is a field name or method name.
All other identifiers are not exported.
这个 Go 语言规范定义的访问权限控制方法。
但是有没有办法突破这个限制呢?
突破可以从两个方向来讨论: 将 exported 类型变为其它 package 不可访问; 将 unexported 的类型变为其它 package 可访问。
1.1. 将 exported 类型变为其它 package 不可访问
至少有一个办法可以将 package 中 exported 的函数、类型变为其它 package 不可访问, 那就是定义一个 internal
package, 将这些 package 放在 internal
package 之下。
Go 语言本身没有这个限制, 这是通过 go 命令实现的。最早这个特性是在 go 1.4 版本中引入的, 相关的细节可以查看文档: design document
这个规则是这样的:
An import of a path containing the element “internal” is disallowed if the importing code is outside the tree rooted at the parent of the “internal” directory.
也就是 internal
包下的 exported 类型只能由 internal
所在的 package (internal
的 parent) 为 root 的 package 所访问。
举例来说:
/a/b/c/internal/d/e/f
可以被/a/b/c
import, 不能被/a/b/g
import.$GOROOT/src/pkg/internal/xxx
只可以被标准库 import ($GOROOT/src/
).$GOROOT/src/pkg/net/http/internal
只可以被net/http
和net/http/*
import.$GOPATH/src/mypkg/internal/foo
只能被$GOPATH/src/mypkg
import.
1.2. 访问其它 package 中的私有方法
如果你查看 Go 标准库的的代码, 比如 time/sleep.go
文件, 你会发现一些奇怪的函数, 如 Sleep
:
func Sleep(d Duration)
这个函数我们经常会用到, 也就是 time.Sleep
函数, 但是这个函数并没有函数体, 而且同样的目录下也没有汇编语言的代码实现, 那么, 这个函数在哪里定义的?
依照规范, 一个只有函数声明的函数是在 Go 的外部实现的, 我们称之为 external function
。
实际上, 这个"外部函数"也是在 Go 标准库中实现的, 它是 runtime 中的一个 unexported 的函数:
//go:linkname timeSleep time.Sleep
func timeSleep(ns int64) {
if ns <= 0 {
return
}
t := getg().timer
if t == nil {
t = new(timer)
getg().timer = t
}
......
}
事实上, runtime 为其它 package 中定义了很多的函数, 比如 sync
、net
中的一些函数, 你可以通过命令 grep linkname /usr/local/go/src/runtime/*.go
查找这些函数。
我们会有两个疑问: 一是为什么这些函数要定义在 runtime package 中, 而是这个机制到底是怎么实现的?
将相关的函数定义在 runtime
中的好处是, 它们可以访问 runtime package 中 unexported 的类型, 比如 getp
函数等, 相当于往 runtime package 打入一个"叛徒", 通过"叛徒"可以访问 runtime package 的私有对象。同时, 这些"叛徒"函数尽管被声明为 unexported, 还是可以在其它 package 中访问。
第二个问题, 其实是 Go 的 go:linkname
这个指令发挥的作用, 它的格式如下:
//go:linkname localname importpath.name
Go 文档说明了这个指令的作用:
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”.
这个指令告诉编译器为函数或者变量 localname
使用 importpath.name
作为目标文件的符号名。因为这个指令破坏了类型系统和包的模块化, 所以它只能在 import “unsafe” 的情况下才能使用。
importpath.name
可以是这种格式: a/b/c/d/apkg.foo
, 这样在 package a/b/c/d/apkg
中就可以使用这个函数 foo
了。
举个例子, 假设我们的 package 布局如下:
├── a
│ └── a.go
├── b
│ ├── b.go
│ └── internal.s
└── main
└── main.go
package a
定义了私有的方法, 并加上 go:linkname
指令, package b
可以调用 package a
的私有方法。 main.go
测试访问 b
中的函数。
首先看看 a.go
中的实现:
a.go
package a
import (
_ "unsafe"
)
//go:linkname say a.say
//go:nosplit
func say(name string) string {
return "hello, " + name
}
//go:linkname say2 github.com/smallnest/private/b.Hi
//go:nosplit
func say2(name string) string {
return "hi, " + name
}
它定义了两个方法, 符号名分别为 a.say
和 github.com/smallnest/private/b.Hi
。
这个不同的符号名的方式会影响 b
中的使用。
b.go
package b
import (
_ "unsafe"
_ "github.com/smallnest/private/a"
)
//go:linkname say a.say
func say(name string) string
func Greet(name string) string {
return say(name)
}
func Hi(name string) string
在 b
中, 如果想使用符号 a.say
, 你还是需要 go:linkname
, 告诉编译器这个函数的符号为 a.say
。对于 Hi
函数, 我们不需要 go:linkname
指令, 因为在 a.go
中我们定义的符号名称恰巧就是这个 package.funcname
。
注意, 你需要引入 package unsafe
, 并且在 b.go
还需要 import package a.
你可以在 main.go
中调用 b
:
package main
import (
"fmt"
"github.com/smallnest/private/b"
)
func main() {
s := b.Greet("world")
fmt.Println(s)
s = b.Hi("world")
fmt.Println(s)
}
但是, 如果你 go run main.go
, 你不会得到正确的结果, 而是会出错:
main go run main.go
# github.com/smallnest/private/b
../b/b.go:10: missing function body for "say"
../b/b.go:16: missing function body for "Hi"
难道我们前面讲的都是错的吗?
这里有一个技巧, 你在 package b
下创建一个空的文件, w
文件名随意, 只要文件后缀为 .s
, 再运行一下 go run main.go
:
main go run main.go
hello, world
hi, world
原因在于 Go 在编译的时候会启用 -complete
编译器 flag, 它要求所有的函数必需包含函数体。创建一个空的汇编语言文件绕过这个限制。
当然, 一般情况下我们不会用到本文所列出的两种突破方式, 只有在很稀少的情况下, 为了更好地组织我们的代码, 我们才会有选择的采用这两种方法。至少, 作为一个 Go 开发者, 你会记住有两种突破方法, 可以打破 Go 语言规范中关于权限的限制。
一个应用的例子是可以在代码中访问 sync.runtime_registerPoolCleanup
, 因为它有明确的 linkname
//go:linkname runtime_registerPoolCleanup
sync.runtime_registerPoolCleanup
func runtime_registerPoolCleanup(cleanup func())
1.3. 访问其它 package 中的 struct 私有字段
再额外附送一个技巧, 可以访问其它 package struct 的私有字段。
当然正常情况下 struct 的私有字段并没有 export, 所以在其它 package 是不能正常访问。通过使用 refect
, 可以访问 struct 的私有字段:
import (
"fmt"
"reflect"
"github.com/smallnest/private/c"
)
func ChangeFoo(f *c.Foo) {
v := reflect.ValueOf(f)
x := v.Elem().FieldByName("x")
fmt.Println(x.Int())
//panic: reflect: reflect.Value.SetInt using value obtained using unexported field
//x.SetInt(100)
fmt.Println(x.Int())
y := v.Elem().FieldByName("Y")
y.SetString("world")
fmt.Println(f.Y)
}
但是你不能设置私有字段的值, 否则会 panic, 这是因为 SetXXX
会首先使用 v.mustBeAssignable()
检查字段是否是 exported 的。
当然, 还可以通过"指针"的方式获取字段的地址, 通过地址获取数据或者设置数据。
还是用相同的例子:
c.go
package c
type Foo struct {
x int
Y string
}
func (f Foo) X() int {
return f.x
}
func New(x int, y string) *Foo {
return &Foo{x: x, Y: y}
}
在 package d 中访问:
d.go
package d
import (
"fmt"
"unsafe"
"github.com/smallnest/private/c"
)
func ChangeFoo(f *c.Foo) {
p := unsafe.Pointer(f)
// 事先获取或者通过 reflect 获得
// 本例中是第一个字段, 所以 offset=0
offset := uintptr(0)
ptr2x := (*int)(unsafe.Pointer(uintptr(p) + offset))
fmt.Println(*ptr2x)
*ptr2x = 100
fmt.Println(f.X())
}
1.4. 更 hack 的方法
如果你还不满足, 那么我再赠送一个更 hack 的方法, 但是这个也有点限制, 就是你腰调用的方法应该在之前的某处调用过。
这是 Alan Pierce 提供了一个方法。runtime/symtab.go
中保存了符号表, 通过一些技巧 (go:linkname)
, 能访问它的私有方法, 查找到想要调用的函数, 然后就可以调用了, Alan 将相关的代码写成了一个库, 方便调用: go-forceexport
。
使用方法如下:
var timeNow func() (int64, int32)
err := forceexport.GetFunc(&timeNow, "time.now")
if err != nil {
// Handle errors if you care about name possibly being invalid.
}
// Calls the actual time.now function.
sec, nsec := timeNow()
我在使用的过程中发现只有相应的方法在某处调用过, 符号表中才有这个函数的信息, forceexport.GetFunc
才会返回对应的函数。
另外, 这是一个非常 hack 的方式, 不保证 Go 将来的版本是否还能使用, 仅供嬉戏之用, 慎用在产品代码中。