Skip to content

Instantly share code, notes, and snippets.

@fzdwx
Created December 3, 2022 06:02
Show Gist options
  • Save fzdwx/3ccaefe0d543223b9e66f617a2a08a92 to your computer and use it in GitHub Desktop.
Save fzdwx/3ccaefe0d543223b9e66f617a2a08a92 to your computer and use it in GitHub Desktop.
go ctx react impl
func resSearch(ctx context.Context, hostname string, rtype, class int) ([]dnsmessage.Resource, error) {
if ctx.Done() == nil {
return cgoResSearch(hostname, rtype, class)
}
type result struct {
err error
res []dnsmessage.Resource
}
res := make(chan result, 1)
go func() {
r, err := cgoResSearch(hostname, rtype, class)
res <- result{
err: err,
res: r,
}
}()
select {
case res := <-res:
return res.res, res.err
case <-ctx.Done():
return nil, mapErr(ctx.Err())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment