Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CanUpdate ¶
func CanUpdate(v interface{}) bool
CanUpdate returns true when the given value is an initial value of its type
Example ¶
package main
import (
"fmt"
"github.com/creasty/defaults"
)
func main() {
fmt.Println(defaults.CanUpdate(0), defaults.CanUpdate(123))
fmt.Println(defaults.CanUpdate(""), defaults.CanUpdate("hello"))
}
Output: true false true false
func MustSet ¶ added in v1.5.1
func MustSet(ptr interface{})
MustSet function is a wrapper of Set function It will call Set and panic if err not equals nil.
func Set ¶
func Set(ptr interface{}) error
Set initializes members in a struct referenced by a pointer. Maps and slices are initialized by `make` and other primitive types are set with default values. `ptr` should be a struct pointer
Example ¶
package main
import (
"fmt"
"time"
"github.com/creasty/defaults"
)
func main() {
type Server struct {
Host string `default:"localhost"`
Port int `default:"8080"`
Timeout time.Duration `default:"30s"`
Tags []string `default:"[\"web\"]"`
Labels map[string]string `default:"{\"env\": \"dev\"}"`
}
var server Server
if err := defaults.Set(&server); err != nil {
panic(err)
}
fmt.Printf("%s:%d\n", server.Host, server.Port)
fmt.Println(server.Timeout)
fmt.Println(server.Tags, server.Labels)
}
Output: localhost:8080 30s [web] map[env:dev]
Example (Nested) ¶
package main
import (
"fmt"
"github.com/creasty/defaults"
)
func main() {
type Retry struct {
Attempts int `default:"3"`
}
type Client struct {
// A struct is descended into with or without a tag, and a pointer needs one to be
// allocated at all.
Retry Retry
Fallback *Retry `default:"{}"`
Disabled *Retry
}
var client Client
if err := defaults.Set(&client); err != nil {
panic(err)
}
fmt.Println(client.Retry.Attempts)
fmt.Println(client.Fallback.Attempts)
fmt.Println(client.Disabled == nil)
}
Output: 3 3 true
Types ¶
type Setter ¶
type Setter interface {
SetDefaults()
}
Setter is an interface for setting default values
Example ¶
package main
import (
"fmt"
"time"
"github.com/creasty/defaults"
)
// Config implements defaults.Setter for ExampleSetter below.
type Config struct {
Retries int `default:"3"`
Backoff time.Duration `default:"-"`
}
// SetDefaults derives a default that a tag cannot express, and CanUpdate keeps it from overriding
// what the caller already provided.
func (c *Config) SetDefaults() {
if defaults.CanUpdate(c.Backoff) {
c.Backoff = time.Duration(c.Retries) * time.Second
}
}
func main() {
var config Config
if err := defaults.Set(&config); err != nil {
panic(err)
}
fmt.Println(config.Retries, config.Backoff)
// A value the caller supplied is left alone.
custom := Config{Backoff: time.Minute}
if err := defaults.Set(&custom); err != nil {
panic(err)
}
fmt.Println(custom.Retries, custom.Backoff)
}
Output: 3 3s 3 1m0s
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
fixture
Package fixture provides types declared outside the test package, so tests can observe how Set behaves across a package boundary.
|
Package fixture provides types declared outside the test package, so tests can observe how Set behaves across a package boundary. |
Click to show internal directories.
Click to hide internal directories.