jeudi 23 août 2018

How to extract field from interface

I would like to extract the time from the obj variable i have create trough a method of which you can pass multiple types(hence interface). i am using go for a language and tried to extract using reflection but that was a fail. Thanks in advanced for anyone who can solve this one if possible.

type Soft struct { Create *time.Time Delete *string Update *string }

                        type Obj struct {
                            Name string
                            Soft Soft
                        }

                        func main() {
                            var soft Soft
                            n := time.Now().Format(time.RFC3339)
                            now, _ := time.Parse(time.RFC3339, n)
                            nowUpdate := "update"
                            nowDelete := "delete"
                            soft.Create = &now
                            soft.Update = &nowUpdate
                            soft.Delete = &nowDelete

                            var obj Obj
                            obj.Name = "hi"
                            obj.Soft = soft
                            Extract(obj)
                        }

                        func Extract(a interface{}) {
                            aValue := reflect.ValueOf(a)
                            //i would like to extract the field named Soft from this and Convert it into time.Time.
                        }





Aucun commentaire:

Enregistrer un commentaire