Skip to content

Instantly share code, notes, and snippets.

@thomasjo
Created August 19, 2021 07:57
Show Gist options
  • Save thomasjo/5db91fe21c68cf30cc3d7e5e8bcdfdb1 to your computer and use it in GitHub Desktop.
Save thomasjo/5db91fe21c68cf30cc3d7e5e8bcdfdb1 to your computer and use it in GitHub Desktop.
Go + YAML "string array" implementation
// Source: https://github.com/go-yaml/yaml/issues/100#issuecomment-901604971
type StringArray []string
func (a *StringArray) UnmarshalYAML(value *yaml.Node) error {
var multi []string
err := value.Decode(&multi)
if err != nil {
var single string
err := value.Decode(&single)
if err != nil {
return err
}
*a = []string{single}
} else {
*a = multi
}
return nil
}
type Data struct {
Field StringArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment