Advertisement
Guest User

Untitled

a guest
Sep 17th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.85 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "fmt"
  6.     "reflect"
  7. )
  8.  
  9. // Common s
  10. type Common struct {
  11.     Name   string      `json:"name"`
  12.     Config interface{} `json:"config"`
  13. }
  14.  
  15. // PodConf s
  16. type PodConf struct {
  17.     ID string `json:"id"`
  18. }
  19.  
  20. func getConf() interface{} {
  21.     //var c *PodConf
  22.     //return c
  23.     return new(PodConf)
  24. }
  25.  
  26. func main() {
  27.     data := []byte(`{"name":"kek","config":{"id":"keklelel"}}`)
  28.     c := Common{
  29.         Config: getConf(),
  30.     }
  31.     rv := reflect.ValueOf(&c)
  32.     fmt.Println(rv.Elem().Kind())
  33.     rv = rv.Elem()
  34.     fmt.Println(rv)
  35.  
  36.     switch c.Config.(type) {
  37.     case *PodConf:
  38.         fmt.Println("podconf")
  39.     default:
  40.         fmt.Println("kek")
  41.     }
  42.     err := json.Unmarshal(data, &c)
  43.     if err != nil {
  44.         fmt.Println(err)
  45.     }
  46.     fmt.Println(c, c.Config)
  47.     switch c.Config.(type) {
  48.     case *PodConf:
  49.         fmt.Println("podconf")
  50.     default:
  51.         fmt.Println("kek")
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement