Skip to content

Commit

Permalink
adds usage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jritsema committed Oct 13, 2020
1 parent b5597f4 commit fcedf6b
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,56 @@ A kitchen sink of Go tools that I've found useful. Uses only the standard librar
go get github.com/jritsema/gotoolbox
```

### utils

```go
package main

import utils "github.com/jritsema/gotoolbox"

func main() {

s := []string{"a","b","c"}
if utils.SliceContains(&s, "b") {
fmt.Println("b exists")
}

err := utils.Retry(3, 1, func() error {
return callBrittleAPI()
})
if err != nil {
fmt.Println("callBrittleAPI failed after 3 retries: %w", err)
}

config, err := utils.ReadJSONFile("config.json")
if err != nil {
fmt.Println("error reading json file: %w", err)
}
}
```

#### web framework

```go
package main

import "github.com/jritsema/gotoolbox/web"

type Data struct {
Hello string `json:"hello"`
}

func hello(r *http.Request) *web.Response {
return web.DataJSON(http.StatusOK, Data{Hello: "world"}, nil)
}

func main() {
mux := http.NewServeMux()
mux.Handle("/hello", web.Action(hello))
http.ListenAndServe(":8080", mux)
}
```

### development

```
Expand Down

0 comments on commit fcedf6b

Please sign in to comment.