Skip to content

Commit

Permalink
Publish blog on configuring options in Go, closes #41
Browse files Browse the repository at this point in the history
  • Loading branch information
rednafi committed Sep 6, 2023
1 parent bc0eb50 commit f574220
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions content/go/configure_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ c := &src.Style{
```

In this case, the field that wasn't passed will assume the corresponding zero value. For
instance `Bg` will be initialized as an empty string. However, this pattern puts the
instance, `Bg` will be initialized as an empty string. However, this pattern puts the
responsibilty of retaining API compatibity on the users' shoulders. So if your code is meant
for external use, there are better ways to achieve option configurability.

Expand Down Expand Up @@ -106,8 +106,10 @@ c := src.NewStyle(
Display(c, "Hello, World!")
```

If a new field is added to `Style`, update `NewStyle` to have a default value for it. This
avoids breaking users' code as long as the factory function's signature doesn't change.
If a new field is added to `Style`, update `NewStyle` to have a sensible default value for
it or initialize the struct with named parameters to set the optional fields to their
respective zero values. This avoids breaking users' code as long as the factory function's
signature doesn't change.

```go
package src
Expand Down Expand Up @@ -264,6 +266,7 @@ func WithZigzag(zigzag bool) styleoption {

// Options are variadic but the required fiels must be passed
func NewStyle(fg, bg string, options ...styleoption) *style {
// You can also intialize the optional values explicitly
s := &style{fg: fg, bg: bg}
for _, opt := range options {
opt(s)
Expand Down

0 comments on commit f574220

Please sign in to comment.