Created
October 13, 2022 14:04
-
-
Save michaelneuder/95b26032b56f8e6e1dbaec4665f4764f to your computer and use it in GitHub Desktop.
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
package main | |
import "fmt" | |
type callbackOpts struct { | |
field string | |
} | |
type retType1 struct { | |
retField string | |
} | |
type retType2 struct { | |
retField string | |
} | |
type callbackFn func(callbackOpts) (interface{}, error) | |
func Run(cb callbackFn, cbOpts callbackOpts) error { | |
// Do a bunch of stuff. | |
retVal, err := cb(cbOpts) | |
if err != nil { | |
return err | |
} | |
fmt.Printf("ret val: %v\n", retVal) | |
return nil | |
} | |
func callbackFn1Impl(cbOpts callbackOpts) (interface{}, error) { | |
fmt.Printf("callback function 1 with options: %v\n", cbOpts) | |
return &retType1{ | |
retField: cbOpts.field, | |
}, nil | |
} | |
func callbackFn2Impl(cbOpts callbackOpts) (interface{}, error) { | |
fmt.Printf("callback function 2 with options: %v\n", cbOpts) | |
return &retType2{ | |
retField: cbOpts.field, | |
}, nil | |
} | |
func main() { | |
fmt.Println("Running callback #1...") | |
cbOpts1 := callbackOpts{ | |
field: "test option 1", | |
} | |
if err := Run(callbackFn1Impl, cbOpts1); err != nil { | |
fmt.Printf("error: %v\n", err) | |
return | |
} | |
fmt.Println("Running callback #2...") | |
cbOpts2 := callbackOpts{ | |
field: "test option 2", | |
} | |
if err := Run(callbackFn2Impl, cbOpts2); err != nil { | |
fmt.Printf("error: %v\n", err) | |
return | |
} | |
fmt.Println("success!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment