https://github.com/yanghuan/go-delegate
A delegate implementation of Go, similar to a delegate in C#
https://github.com/yanghuan/go-delegate
Last synced: 7 months ago
JSON representation
A delegate implementation of Go, similar to a delegate in C#
- Host: GitHub
- URL: https://github.com/yanghuan/go-delegate
- Owner: yanghuan
- License: apache-2.0
- Created: 2022-12-05T02:58:37.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-26T06:41:40.000Z (over 2 years ago)
- Last Synced: 2025-01-20T09:46:22.793Z (9 months ago)
- Language: Go
- Size: 21.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-delegate
A multicast delegate implementation of Go, similar to [delegate in C#](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/)## Example
no generics & variadic function
```
func f1(args ...interface{}) {
println("f1")
}func f2(args ...interface{}) {
println("f2")
}func main() {
d := Delegate{}
d = d.Combine(f1)
d = d.Combine(f2)
d.Invoke()
}
```generics & parameters
```
func f() {
println("f")
}func f1(a int) {
println("f1", a)
}func f2(a, b int) {
println("f2", a, b)
}func main() {
d := Action{}
d = d.Combine(f)
d.Invoke()d1 := Action1[int]{}
d1 = d.Combine(f1)
d1.Invoke(1)
d2 := Action2[int, int]{}
d2 = d.Combine(f2)
d2.Invoke(1, 2)
}
```There is Action, Action1, ... Action7, that binds no parameters and up to 7 parameters.