1
- # Async (Experimental)
1
+ # Fillmore Labs Async (Experimental)
2
2
3
3
[ ![ Go Reference] ( https://pkg.go.dev/badge/fillmore-labs.com/exp/async.svg )] ( https://pkg.go.dev/fillmore-labs.com/exp/async )
4
4
[ ![ Build Status] ( https://badge.buildkite.com/06fc8f7bdcfc5c380ea0c7c8bb92a7cee8b1676b841f3c65c8.svg )] ( https://buildkite.com/fillmore-labs/async-exp )
5
+ [ ![ GitHub Workflow] ( https://github.com/fillmore-labs/exp-async/actions/workflows/test.yml/badge.svg?branch=main )] ( https://github.com/fillmore-labs/async-exp/actions/workflows/test.yml )
5
6
[ ![ Test Coverage] ( https://codecov.io/gh/fillmore-labs/async-exp/graph/badge.svg?token=GQUJA8PKJI )] ( https://codecov.io/gh/fillmore-labs/async-exp )
6
7
[ ![ Maintainability] ( https://api.codeclimate.com/v1/badges/72fe9626fb821fc70251/maintainability )] ( https://codeclimate.com/github/fillmore-labs/async-exp/maintainability )
7
8
[ ![ Go Report Card] ( https://goreportcard.com/badge/fillmore-labs.com/exp/async )] ( https://goreportcard.com/report/fillmore-labs.com/exp/async )
8
- [ ![ License] ( https://img.shields.io/github/license/fillmore-labs/exp-async )] ( https://github.com/fillmore-labs/exp-async/blob/main/ LICENSE )
9
+ [ ![ License] ( https://img.shields.io/github/license/fillmore-labs/exp-async )] ( https://www.apache.org/licenses/ LICENSE-2.0 )
9
10
10
11
The ` async ` package provides interfaces and utilities for writing asynchronous code in Go.
11
12
@@ -36,10 +37,10 @@ address (see [GetMyIP](#getmyip) for an example).
36
37
Now you can do
37
38
38
39
``` go
39
- ctx , cancel := context.WithTimeout (context.Background (), timeout )
40
+ ctx , cancel := context.WithTimeout (context.Background (), 5 *time. Second )
40
41
defer cancel ()
41
42
42
- future := async.NewAsyncFuture (func () (string , error ) {
43
+ future := async.NewFutureAsync (func () (string , error ) {
43
44
return getMyIP (ctx)
44
45
})
45
46
```
@@ -48,9 +49,9 @@ and elsewhere in your program, even in a different goroutine
48
49
49
50
``` go
50
51
if ip , err := future.Wait (ctx); err == nil {
51
- log. Printf ( " My IP is %s " , ip)
52
+ slog. Info ( " Found IP" , " ip " , ip)
52
53
} else {
53
- log. Printf ( " Error fetching IP: %v " , err)
54
+ slog. Error ( " Failed to fetch IP " , " error " , err)
54
55
}
55
56
```
56
57
@@ -61,53 +62,30 @@ decoupling query construction from result processing.
61
62
Sample code to retrieve your IP address:
62
63
63
64
``` go
64
- const (
65
- serverURL = " https://httpbin.org/ip"
66
- timeout = 2 * time.Second
67
- )
68
-
69
- type IPResponse struct {
70
- Origin string ` json:"origin"`
71
- }
65
+ const serverURL = " https://httpbin.org/ip"
72
66
73
67
func getMyIP (ctx context .Context ) (string , error ) {
74
- resp , err := sendRequest (ctx)
68
+ req , err := http. NewRequestWithContext (ctx, http. MethodGet , serverURL, nil )
75
69
if err != nil {
76
70
return " " , err
77
71
}
72
+ req.Header .Set (" Accept" , " application/json" )
78
73
79
- ipResponse , err := decodeResponse (resp )
74
+ resp , err := http. DefaultClient . Do (req )
80
75
if err != nil {
81
76
return " " , err
82
77
}
78
+ defer func () { _ = resp.Body .Close () }()
83
79
84
- return ipResponse.Origin , nil
85
- }
80
+ ipResponse := &struct {
81
+ Origin string ` json:"origin"`
82
+ }{}
86
83
87
- func sendRequest (ctx context .Context ) (*http .Response , error ) {
88
- req , err := http.NewRequestWithContext (ctx, http.MethodGet , serverURL, nil )
89
- if err != nil {
90
- return nil , err
91
- }
92
- req.Header .Set (" Accept" , " application/json" )
93
-
94
- return http.DefaultClient .Do (req)
95
- }
96
-
97
- func decodeResponse (response *http .Response ) (*IPResponse , error ) {
98
- body , err := io.ReadAll (response.Body )
99
- _ = response.Body .Close ()
100
- if err != nil {
101
- return nil , err
102
- }
103
-
104
- ipResponse := &IPResponse{}
105
- err = json.Unmarshal (body, ipResponse)
106
- if err != nil {
107
- return nil , err
84
+ if err := json.NewDecoder (resp.Body ).Decode (ipResponse); err != nil {
85
+ return " " , err
108
86
}
109
87
110
- return ipResponse, nil
88
+ return ipResponse. Origin , nil
111
89
}
112
90
```
113
91
0 commit comments