Skip to content

Instantly share code, notes, and snippets.

View arindamroynitw's full-sized avatar

Arindam Roy arindamroynitw

View GitHub Profile
@arindamroynitw
arindamroynitw / keyop.go
Created August 26, 2019 09:16
Implementation of Operators
import (
"fmt"
"strconv"
"strings"
)
type keyOp struct {
Template string
}
@arindamroynitw
arindamroynitw / operators.go
Created August 26, 2019 09:12
operator interface
type Operators interface {
Generate(int, int) string
Degenerate(string) (int, int, error)
}
@arindamroynitw
arindamroynitw / main.go
Created August 15, 2019 18:17
Main function with context timeout - Timeouts in Go
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 {
@arindamroynitw
arindamroynitw / Call.go
Created August 15, 2019 18:11
New Http Response Function with context - Timeouts in Go
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
}
}
@arindamroynitw
arindamroynitw / NetworkCall.go
Last active August 15, 2019 18:10
helper function - Timeouts in Go
type CallResponse struct {
Resp *Response
Err error
}
func helper(ctx context.Context) <-chan *CallResponse {
respChan := make(chan *CallResponse, 1)
go func() {
@arindamroynitw
arindamroynitw / main.go
Created August 15, 2019 17:46
Main file for Demo - Timeouts in Go
import (
"fmt"
)
func main() {
res, err := GetHttpResponse()
if err != nil {
fmt.Printf("err %v", err)
} else {
@arindamroynitw
arindamroynitw / NetworkCall.go
Last active August 15, 2019 17:43
Function to get response from mock server - Timeouts in Golang
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func GetHttpResponse() (*Response, error) {
resp, err := http.Get("https://jsonplaceholder.typicode.com/todos/1")
@arindamroynitw
arindamroynitw / Response.go
Created August 15, 2019 17:37
Structure for Response From Mock Service - Timeouts in Golang
type Response struct {
UserID int `json:"userId"`
ID int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}