-
-
Save fakeboboliu/dfc25fe4a1d910333d743e873e39072c to your computer and use it in GitHub Desktop.
closure example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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