An open API service indexing awesome lists of open source software.

https://github.com/yakuter/optinator

Idiomatic way to fill structs with options logic
https://github.com/yakuter/optinator

go golang options struct

Last synced: 11 months ago
JSON representation

Idiomatic way to fill structs with options logic

Awesome Lists containing this project

README

          

# optinator
Go packages are generally start with a main struct and the package initiates and fills that struct in the beginning. There are so many ways to fill that struct.

In this repo I wanted to show and idiomatic way to fill a struct. This is generally used with "options" parameters. So I called it optinator. Hope you find it helpful.

Example usage:
```go
func main() {
req := NewReq(
WithAddress("https://yakuter.com"),
WithTimeout(30*time.Second),
WithContentType("application/json"),
)

fmt.Printf("%+v", req)
}
```

Sources:
- [Different Ways to Initialize Go structs](https://asankov.dev/blog/2022/01/29/different-ways-to-initialize-go-structs/)
- [github.com/sethvargo/go-githubactions](https://github.com/sethvargo/go-githubactions/blob/main/options.go)
- [github.com/binalyze/httpreq](https://github.com/binalyze/httpreq)