Skip to content

Instantly share code, notes, and snippets.

@fakeboboliu
Last active May 16, 2021 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fakeboboliu/dfc25fe4a1d910333d743e873e39072c to your computer and use it in GitHub Desktop.
Save fakeboboliu/dfc25fe4a1d910333d743e873e39072c to your computer and use it in GitHub Desktop.
closure example
// https://github.com/XTLS/Xray-core/blob/main/proxy/vmess/aead/kdf.go 实现示意
package main
import "fmt"
type caller struct {
F func()
key string
}
func (c caller) Call() {
if c.key != "" {
fmt.Println(c.key)
}
c.F()
}
func newCaller(f func(), key string) *caller {
return &caller{F: f, key: key}
}
func main() {
callee := &caller{F: func() { fmt.Println(1) }}
words := []string{"bb", "dd", "jj"}
for _, word := range words {
callee = newCaller(callee.Call, word)
}
callee.Call() // jj\ndd\nbb\n1\n
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment