Skip to content

Instantly share code, notes, and snippets.

@ka2n
Last active October 18, 2024 22:41
Show Gist options
  • Save ka2n/4457eacdb6c986624eb29cc02fe8d31c to your computer and use it in GitHub Desktop.
Save ka2n/4457eacdb6c986624eb29cc02fe8d31c to your computer and use it in GitHub Desktop.
Parse yaml with dynamic key name usign go.
package main
import (
"io/ioutil"
"log"
"gopkg.in/yaml.v2"
)
type Config struct {
Key string `yaml:"key"`
}
func main() {
b, err := ioutil.ReadFile("./settings.yml")
if err != nil {
log.Fatal(err)
}
var cfg map[string]Config
if err := yaml.Unmarshal(b, &cfg); err != nil {
log.Fatal(err)
}
log.Printf("%+v", cfg)
}
development:
key: arg_for_development
production:
key: arg_for_production
env1:
key: arg_for_env1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment