Advertisement
Guest User

Untitled

a guest
Nov 25th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "flag"
  5. "fmt"
  6. "reflect"
  7. )
  8.  
  9. type AppConfig struct {
  10. Pg string `cli:"pg" env:"PG" default:"host=host.local dbname=db user=user password=password" description:"Connection to PostgreSQL"`
  11. }
  12.  
  13. func main() {
  14. config := &AppConfig{}
  15. GetConfig(config)
  16. fmt.Println(config.Pg)
  17. }
  18. func GetConfig(config interface{}) interface{} {
  19. e := reflect.ValueOf(config).Elem()
  20. t := e.Type()
  21. flag.StringVar(
  22. e.Field(0).Interface().(*string),
  23. t.Field(0).Tag.Get("cli"),
  24. t.Field(0).Tag.Get("default"),
  25. t.Field(0).Tag.Get("description"))
  26. flag.Parse()
  27. return config
  28. }
  29.  
  30. panic: interface conversion: interface {} is string, not *string
  31.  
  32. main.go:22: cannot take the address of e.Field(0).Interface().(string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement