mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
commit
f989f44898
@ -1,141 +0,0 @@
|
||||
Compile-time assertions in Go
|
||||
============================================================
|
||||
|
||||
|
||||
This post is about a little-known way to make compile-time assertions in Go. You probably shouldn’t use it, but it is interesting to know about.
|
||||
|
||||
As a warm-up, here’s a fairly well-known form of compile-time assertions in Go: Interface satisfaction checks.
|
||||
|
||||
In this code ([playground][1]), the `var _ =` line ensures that type `W` is a `stringWriter`, as checked for by [`io.WriteString`][2].
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "io"
|
||||
|
||||
type W struct{}
|
||||
|
||||
func (w W) Write(b []byte) (int, error) { return len(b), nil }
|
||||
func (w W) WriteString(s string) (int, error) { return len(s), nil }
|
||||
|
||||
type stringWriter interface {
|
||||
WriteString(string) (int, error)
|
||||
}
|
||||
|
||||
var _ stringWriter = W{}
|
||||
|
||||
func main() {
|
||||
var w W
|
||||
io.WriteString(w, "very long string")
|
||||
}
|
||||
```
|
||||
|
||||
If you comment out `W`’s `WriteString` method, the code will not compile:
|
||||
|
||||
```
|
||||
main.go:14: cannot use W literal (type W) as type stringWriter in assignment:
|
||||
W does not implement stringWriter (missing WriteString method)
|
||||
```
|
||||
|
||||
This is useful. For most types that satisfy both `io.Writer` and `stringWriter`, if you eliminate the `WriteString` method, everything will continue to work as it did before, but with worse performance.
|
||||
|
||||
Rather than trying to write a fragile test for a performance regression using [`testing.T.AllocsPerRun`][3], you can simply protect your code with a compile-time assertion.
|
||||
|
||||
Here’s [a real world example of this technique from package io][4].
|
||||
|
||||
* * *
|
||||
|
||||
OK, onward to obscurity!
|
||||
|
||||
Interface satisfaction checks are great. But what if you wanted to check a plain old boolean expression, like `1+1==2`?
|
||||
|
||||
Consider this code ([playground][5]):
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "crypto/md5"
|
||||
|
||||
type Hash [16]byte
|
||||
|
||||
func init() {
|
||||
if len(Hash{}) < md5.Size {
|
||||
panic("Hash is too small")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
`Hash` is perhaps some kind of abstracted hash result. The `init` function ensures that it will work with [crypto/md5][6]. If you change `Hash` to be (say) `[8]byte`, it’ll panic when the process starts. However, this is a run-time check. What if we wanted it to fail earlier?
|
||||
|
||||
Here’s how. (There’s no playground link, because this doesn’t work on the playground.)
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "C"
|
||||
|
||||
import "crypto/md5"
|
||||
|
||||
type Hash [16]byte
|
||||
|
||||
func hashIsTooSmall()
|
||||
|
||||
func init() {
|
||||
if len(Hash{}) < md5.Size {
|
||||
hashIsTooSmall()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
Now if you change `Hash` to be `[8]byte`, it will fail during compilation. (Actually, it fails during linking. Close enough for our purposes.)
|
||||
|
||||
```
|
||||
$ go build .
|
||||
# demo
|
||||
main.hashIsTooSmall: call to external function
|
||||
main.init.1: relocation target main.hashIsTooSmall not defined
|
||||
main.init.1: undefined: "main.hashIsTooSmall"
|
||||
```
|
||||
|
||||
What’s going on here?
|
||||
|
||||
`hashIsTooSmall` is [declared without a function body][7]. The compiler assumes that someone else will provide an implementation, perhaps an assembly routine.
|
||||
|
||||
When the compiler can prove that `len(Hash{}) < md5.Size`, it eliminates the code inside the if statement. As a result, no one uses the function `hashIsTooSmall`, so the linker eliminates it. No harm done. As soon as the assertion fails, the code inside the if statement is preserved.`hashIsTooSmall` can’t be eliminated. The linker then notices that no one else has provided an implementation for the function and fails with an error, which was the goal.
|
||||
|
||||
One last oddity: Why `import "C"`? The go tool knows that in normal Go code, all functions must have bodies, and instructs the compiler to enforce that. By switching to cgo, we remove that check. (If you run `go build -x` on the code above, without the `import "C"` line, you will see that the compiler is invoked with the `-complete` flag.) An alternative to adding `import "C"` is to [add an empty file called `foo.s` to the package][8].
|
||||
|
||||
I know of only one use of this technique, in the [compiler test suite][9]. There are other [imaginable places to apply it][10], but no one has bothered.
|
||||
|
||||
And that’s probably how it should be. :)
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://commaok.xyz/post/compile-time-assertions
|
||||
|
||||
作者:[Josh Bleecher Snyder][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://twitter.com/commaok
|
||||
[1]:https://play.golang.org/p/MJ6zF1oNsX
|
||||
[2]:https://golang.org/pkg/io/#WriteString
|
||||
[3]:https://golang.org/pkg/testing/#AllocsPerRun
|
||||
[4]:https://github.com/golang/go/blob/go1.8rc2/src/io/multi.go#L72
|
||||
[5]:https://play.golang.org/p/mjIMWsWu4V
|
||||
[6]:https://golang.org/pkg/crypto/md5/
|
||||
[7]:https://golang.org/ref/spec#Function_declarations
|
||||
[8]:https://github.com/golang/go/blob/go1.8rc2/src/os/signal/sig.s
|
||||
[9]:https://github.com/golang/go/blob/go1.8rc2/test/fixedbugs/issue9608.dir/issue9608.go
|
||||
[10]:https://github.com/golang/go/blob/go1.8rc2/src/runtime/hashmap.go#L261
|
141
translated/tech/20170124 Compile-time assertions in Go.md
Normal file
141
translated/tech/20170124 Compile-time assertions in Go.md
Normal file
@ -0,0 +1,141 @@
|
||||
Go 语言编译期断言
|
||||
============================================================
|
||||
|
||||
|
||||
这篇文章是关于一个鲜为人知的方法让 Go 在编译期断言。你可能不会使用它,但是了解一下也很有趣。
|
||||
|
||||
作为一个热身,这里是一个在 Go 中相当知名的编译时断言:接口满意度检查。
|
||||
|
||||
在这段代码([playground][1])中,`var _ =` 行确保类型 `W` 是一个 `stringWriter`,由 [`io.WriteString`][2] 检查。
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "io"
|
||||
|
||||
type W struct{}
|
||||
|
||||
func (w W) Write(b []byte) (int, error) { return len(b), nil }
|
||||
func (w W) WriteString(s string) (int, error) { return len(s), nil }
|
||||
|
||||
type stringWriter interface {
|
||||
WriteString(string) (int, error)
|
||||
}
|
||||
|
||||
var _ stringWriter = W{}
|
||||
|
||||
func main() {
|
||||
var w W
|
||||
io.WriteString(w, "very long string")
|
||||
}
|
||||
```
|
||||
|
||||
如果你注释掉了 `W` 的 `WriteString` 方法,代码将无法编译:
|
||||
|
||||
```
|
||||
main.go:14: cannot use W literal (type W) as type stringWriter in assignment:
|
||||
W does not implement stringWriter (missing WriteString method)
|
||||
```
|
||||
|
||||
这是很有用的。对于大多数同时满足 `io.Writer` 和 `stringWriter` 的类型,如果你删除 `WriteString` 方法,一切都会像以前一样继续工作,但性能较差。
|
||||
|
||||
你可以使用编译时断言保护你的代码,而不是试图使用[`testing.T.AllocsPerRun'][3]为性能回归编写一个脆弱的测试。
|
||||
|
||||
这是[一个实际的 io 包中的技术例子][4]。
|
||||
|
||||
* * *
|
||||
|
||||
好的,让我们隐晦一点!
|
||||
|
||||
接口满意检查是很棒的。但是如果你想检查一个简单的布尔表达式,如 `1 + 1 == 2` ?
|
||||
|
||||
考虑这个代码([playground] [5]):
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "crypto/md5"
|
||||
|
||||
type Hash [16]byte
|
||||
|
||||
func init() {
|
||||
if len(Hash{}) < md5.Size {
|
||||
panic("Hash is too small")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
`Hash` 可能是某种抽象的哈希结果。`init` 函数确保它将与[crypto/md5][6]一起工作。如果你改变 `Hash` 为(也就是)`[8]byte`,它会在进程启动时发生混乱。但是,这是一个运行时检查。如果我们想要早点发现怎么办?
|
||||
|
||||
就是这样。(没有 playground 链接,因为这在 playground 上不起作用。)
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import "C"
|
||||
|
||||
import "crypto/md5"
|
||||
|
||||
type Hash [16]byte
|
||||
|
||||
func hashIsTooSmall()
|
||||
|
||||
func init() {
|
||||
if len(Hash{}) < md5.Size {
|
||||
hashIsTooSmall()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
现在如果你改变 `Hash` 为 `[8]byte`,它将在编译过程中失败。(实际上,它在链接过程中失败。足够接近我们的目标了。)
|
||||
|
||||
```
|
||||
$ go build .
|
||||
# demo
|
||||
main.hashIsTooSmall: call to external function
|
||||
main.init.1: relocation target main.hashIsTooSmall not defined
|
||||
main.init.1: undefined: "main.hashIsTooSmall"
|
||||
```
|
||||
|
||||
这里发生了什么?
|
||||
|
||||
`hashIsTooSmall` 是[一个没有函数体的声明][7]。编译器假定别人将提供一个实现,也许是一个汇编程序。
|
||||
|
||||
当编译器可以证明 `len(Hash {})<md5.Size` 时,它消除了 if 语句中的代码。结果,没有人使用函数 `hashIsTooSmall`,所以链接器会消除它。没有其他损害。一旦断言失败,if 语句中的代码将被保留。不会消除 `hashIsTooSmall`。链接器然后注意到没有人提供了函数的实现然后链接失败,并出现错误,这是我们的目标。
|
||||
|
||||
最后一个奇怪的点:为什么是 `import "C"`? go 工具知道在正常的 Go 代码中,所有函数都必须有主体,并指示编译器强制执行。通过切换到 cgo,我们删除该检查。(如果你在上面的代码中运行 `go build -x',而没有添加 `import "C"` 这行,你会看到编译器是用 `-complete` 标志调用的。)另一种方法是添加 `import "C"` 来[向包中添加一个名为 `foo.s` 的空文件][8]。
|
||||
|
||||
我在[编译器测试套件][9]中只知道这种使用。还有其他[可以发挥想象力的使用][10],但还没有人打扰。
|
||||
|
||||
可能就是这样吧。 :)
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://commaok.xyz/post/compile-time-assertions
|
||||
|
||||
作者:[Josh Bleecher Snyder][a]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://twitter.com/commaok
|
||||
[1]:https://play.golang.org/p/MJ6zF1oNsX
|
||||
[2]:https://golang.org/pkg/io/#WriteString
|
||||
[3]:https://golang.org/pkg/testing/#AllocsPerRun
|
||||
[4]:https://github.com/golang/go/blob/go1.8rc2/src/io/multi.go#L72
|
||||
[5]:https://play.golang.org/p/mjIMWsWu4V
|
||||
[6]:https://golang.org/pkg/crypto/md5/
|
||||
[7]:https://golang.org/ref/spec#Function_declarations
|
||||
[8]:https://github.com/golang/go/blob/go1.8rc2/src/os/signal/sig.s
|
||||
[9]:https://github.com/golang/go/blob/go1.8rc2/test/fixedbugs/issue9608.dir/issue9608.go
|
||||
[10]:https://github.com/golang/go/blob/go1.8rc2/src/runtime/hashmap.go#L261
|
Loading…
Reference in New Issue
Block a user