mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 12:32:21 +00:00
35 lines
630 B
Go
35 lines
630 B
Go
// Copyright © 2018 ChaiShushan <chaishushan{AT}gmail.com>.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
package main
|
|
|
|
import (
|
|
"unsafe"
|
|
)
|
|
|
|
type FunTwiceClosure struct {
|
|
F uintptr
|
|
X int
|
|
}
|
|
|
|
func NewTwiceFunClosure(x int) func() int {
|
|
var p = &FunTwiceClosure{
|
|
F: asmFunTwiceClosureAddr(),
|
|
X: x,
|
|
}
|
|
return ptrToFunc(unsafe.Pointer(p))
|
|
}
|
|
|
|
func ptrToFunc(p unsafe.Pointer) func() int
|
|
|
|
func asmFunTwiceClosureAddr() uintptr
|
|
func asmFunTwiceClosureBody() int
|
|
|
|
func main() {
|
|
fnTwice := NewTwiceFunClosure(1)
|
|
|
|
println(fnTwice()) // 1*2 => 2
|
|
println(fnTwice()) // 2*2 => 4
|
|
println(fnTwice()) // 4*2 => 8
|
|
}
|