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
import ( | |
"fmt" | |
"strconv" | |
"strings" | |
) | |
type keyOp struct { | |
Template string | |
} |
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
type Operators interface { | |
Generate(int, int) string | |
Degenerate(string) (int, int, error) | |
} |
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
import "fmt" | |
func main() { | |
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | |
defer cancel() | |
res, err := GetHttpResponse(ctx) | |
if err != nil { | |
fmt.Printf("err %v", err) | |
} else { |
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
func GetHttpResponse(ctx context.Context) (*Response, error) { | |
select { | |
case <-ctx.Done(): | |
return nil, fmt.Errorf("context timeout, ran out of time") | |
case respChan := <-helper(ctx): | |
return respChan.Resp, respChan.Err | |
} | |
} |
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
type CallResponse struct { | |
Resp *Response | |
Err error | |
} | |
func helper(ctx context.Context) <-chan *CallResponse { | |
respChan := make(chan *CallResponse, 1) | |
go func() { |
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
import ( | |
"fmt" | |
) | |
func main() { | |
res, err := GetHttpResponse() | |
if err != nil { | |
fmt.Printf("err %v", err) | |
} else { |
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
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
) | |
func GetHttpResponse() (*Response, error) { | |
resp, err := http.Get("https://jsonplaceholder.typicode.com/todos/1") |
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
type Response struct { | |
UserID int `json:"userId"` | |
ID int `json:"id"` | |
Title string `json:"title"` | |
Completed bool `json:"completed"` | |
} |
NewerOlder