YAML Encoder
encoders/yaml
The YAML encoder allows you to serialize and deserialize Go structs to YAML, providing a human-readable format that is especially useful for configuration and interoperability with other YAML-based systems.
📦 Installation
Latest version : View all versions
go get github.com/kivigo/encoders/yaml
🚀 Usage
import (
"context"
"log"
"github.com/kivigo/kivigo"
"github.com/kivigo/encoders/yaml"
"github.com/kivigo/backends/badger"
)
type Config struct {
Port int `yaml:"port"`
Host string `yaml:"host"`
}
func main() {
kvStore, _ := badger.New(badger.DefaultOptions("./data"))
defer kvStore.Close()
client, err := kivigo.New(kvStore, kivigo.Option{
Encoder: yaml.New(),
})
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
config := Config{Port: 8080, Host: "localhost"}
// Store with YAML encoder
err = client.Set(ctx, "config", config)
if err != nil {
log.Fatal(err)
}
// Retrieve with YAML encoder
var retrievedConfig Config
err = client.Get(ctx, "config", &retrievedConfig)
if err != nil {
log.Fatal(err)
}
log.Printf("Retrieved config: %+v", retrievedConfig)
}
📝 Notes
- YAML is ideal for configuration data and is widely used in DevOps and cloud-native environments.
- Always use the same encoder for a given key to avoid decoding errors.
- YAML supports comments and complex structures, but may be slower than JSON for very large datasets.
- You can use struct tags (e.g.,`yaml:"field"`) to control serialization.