I am able to create a variable 'model' of type 'Sample' as follows:
type Sample struct {
Id int `jsonapi:"attr,id,omitempty"`
Name string `jsonapi:"attr,name,omitempty"`
}
var model Sample // created successfully
I am able to create it successfully as I already know the struct type (Sample).
However, when I tried to create a similar variable 'a' as follows, I get syntax error:
package main
import (
"fmt"
"reflect"
)
type Sample struct {
Id int `jsonapi:"attr,id,omitempty"`
Name string `jsonapi:"attr,name,omitempty"`
}
func test(m interface{}) {
fmt.Println(reflect.TypeOf(m)) // prints 'main.Sample'
var a reflect.TypeOf(m) // it throws - syntax error: unexpected ( at end of statement
}
func main() {
var model Sample // I have created a model of type Sample
model = Sample{Id: 1, Name: "MAK"}
test(model)
}
Please advise how to create a variable of dynamic type in Go.