DEV Community

Clavin June
Clavin June

Posted on • Originally published at clavinjune.dev on

1 1

Construct Golang Struct Using Optional Function

Sunday Snippet #5 construct golang struct using optional function

Using optional function to allow user optionally set attributes

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
)

// ExampleOptFunc sets Example's optional attribute
type ExampleOptFunc func(*Example)

// WithAttr1 sets Example's OptionalAttr1 as true
func WithAttr1() ExampleOptFunc {
    return func(e *Example) {
        e.OptionalAttr1 = true
    }
}

// WithAttr2 sets Example's OptionalAttr2 as i
func WithAttr2(i int) ExampleOptFunc {
    return func(e *Example) {
        e.OptionalAttr2 = i
    }
}

// WithAttr3 sets Example's OptionalAttr3 as s
func WithAttr3(s string) ExampleOptFunc {
    return func(e *Example) {
        e.OptionalAttr3 = s
    }
}

// Example is an example struct with some optional attributes
type Example struct {
    Name          string `json:"name"`
    OptionalAttr1 bool   `json:"optional_attr1"`
    OptionalAttr2 int    `json:"optional_attr2"`
    OptionalAttr3 string `json:"optional_attr3"`
}

// NewExample creates Example ptr
// requires name
func NewExample(name string, opts ...ExampleOptFunc) *Example {
    e := &Example{
        Name: name,
    }

    for _, opt := range opts {
        opt(e)
    }

    return e
}

// String returns json-encoded string
func (e *Example) String() string {
    var b bytes.Buffer
    enc := json.NewEncoder(&b)
    enc.SetIndent("", "  ")
    _ = enc.Encode(e)
    return b.String()
}

func main() {
    e := NewExample("example",
        WithAttr1(),
        WithAttr2(100),
        WithAttr3("testing"))

    fmt.Println(e)
    // {
    //   "name": "example",
    //   "optional_attr1": true,
    //   "optional_attr2": 100,
    //   "optional_attr3": "testing"
    // }
}
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay