Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "flag"
- "fmt"
- "reflect"
- )
- type AppConfig struct {
- Pg string `cli:"pg" env:"PG" default:"host=host.local dbname=db user=user password=password" description:"Connection to PostgreSQL"`
- }
- func main() {
- config := &AppConfig{}
- GetConfig(config)
- fmt.Println(config.Pg)
- }
- func GetConfig(config interface{}) interface{} {
- e := reflect.ValueOf(config).Elem()
- t := e.Type()
- flag.StringVar(
- e.Field(0).Interface().(*string),
- t.Field(0).Tag.Get("cli"),
- t.Field(0).Tag.Get("default"),
- t.Field(0).Tag.Get("description"))
- flag.Parse()
- return config
- }
- panic: interface conversion: interface {} is string, not *string
- main.go:22: cannot take the address of e.Field(0).Interface().(string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement