Get struct field tag using Go reflect package - reflection

Is it possible to reflect on a field of a struct, and get a reference to its tag values?
For example:
type User struct {
name string `json:name-field`
age int
}
// ...
user := &User{"John Doe The Fourth", 20}
getStructTag(user.name)
// ...
func getStructTag(i interface{}) string{
//get tag from field
}
From what I can see, the usual way to do this is to range over typ.NumField(), and then call field.Tag.Get("tagname").
However, in my use-case, it would be much better to not have to pass the whole struct in.

You don't need to pass in the whole struct, but passing in the value of one of the fields is not sufficient.
In your example user.name field is just a string - the reflect package will have no way of correlating that back to the original struct.
Instead, you need to pass around the reflect.StructField for the given field:
field, ok := reflect.TypeOf(user).Elem().FieldByName("name")
…
tag = string(field.Tag)
Note: we use Elem above because user is a pointer to a struct.
You can play with an example here.

Modifying the above answer can give value of a specific tag
package main
import (
"fmt"
"reflect"
)
type User struct {
Name string `json:"name_field"`
Age int
}
func main() {
user := &User{"John Doe The Fourth", 20}
field, ok := reflect.TypeOf(user).Elem().FieldByName("Name")
if !ok {
panic("Field not found")
}
fmt.Println(getStructTag(field, "json")) //name_field
}
func getStructTag(f reflect.StructField, tagName string) string {
return string(f.Tag.Get(tagName))
}
https://play.golang.org/p/Sb0i7za5Uow

Clean way to list all tags from a struct (using external lib).
External lib: https://github.com/fatih/structs
Example: https://go.dev/play/p/C_yXMdbFYAq
package main
import (
"fmt"
"github.com/fatih/structs"
)
type User struct {
Name string `json:"name_field"`
Age int `json:"age_field"`
}
func main() {
user := &User{}
fields := structs.Fields(user)
for _, field := range fields {
tag := field.Tag("json")
fmt.Println(tag)
}
}
Result:
name_field
age_field

Related

How to set tags while using reflect.New

When creating a new struct from existing struct, tags are not set on the new struct.
For example:
package main
import (
"fmt"
"reflect"
)
type Foo struct {
Bar string `custom:"tag"`
}
func readTag(e interface{}) {
t := reflect.TypeOf(e).Elem()
f, _ := t.FieldByName("Bar")
fmt.Println(f.Tag)
}
func main() {
foo := &Foo{"baz"}
fmt.Println(foo)
readTag(foo)
fooType := reflect.TypeOf(foo).Elem()
newFoo := reflect.New(fooType).Elem()
newFoo.FieldByName("Bar").SetString("baz2")
fmt.Println(newFoo)
readTag(&newFoo)// empty
}
Playground link: https://play.golang.org/p/7-zMPnwQ8Vo
How to set tags while using reflect.New? Is it even possible?
Tags do not belong to instances, tags belong to types.
So when you create new instances of your type, their type will be the same "wearing" the same tags. It doesn't matter if you create new instances with literals or via the reflect package.
The problem in your case is that newFoo is of type reflect.Value, and &newFoo is of type *reflect.Value, it's not a pointer to your struct (not of type *Foo).
If you unwrap the struct value:
newFoo.Interface()
And you pass this, and you make the Elem() call optional (only do it if it's a pointer):
func readTag(e interface{}) {
t := reflect.TypeOf(e)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
f, _ := t.FieldByName("Bar")
fmt.Println(f.Tag)
}
Then you'll get the same tag (try it on the Go Playground):
&{baz}
custom:"tag"
{baz2}
custom:"tag"
You get the same result if you keep the reflect.Value wrapping the struct pointer, and unwrap from it:
newFooPtr := reflect.New(fooType)
newFoo := newFooPtr.Elem()
newFoo.FieldByName("Bar").SetString("baz2")
fmt.Println(newFoo)
readTag(newFooPtr.Interface()) // empty
Then readTag() doesn't need to be modified. Try this version on the Go Playground.

Unable to access values from Pointer receiver

I'm unable to get value from Pointer receiver. It keeps returning memory address.
I'm trying to access values from the pointer receivers from other file in this below format
package types
import (
// "Some product related imports"
"golang.org/x/oauth2"
"time"
)
type TestContext struct {
userId string
}
func (cont *TestContext) GetUserId() string {
return cont.userId
}
I'm trying to solve it through multiple ways but either getting memory address, nil values or error.
Always write clean code:
Name userID not userId.
Name UserID() not GetUserId().
use ctx2 := &myType.myType{} instead of ctx2 := *myType.myType{}.
try this code:
package main
import (
"fmt"
)
type myType struct {
userID string
}
func (cont *myType) UserID() string {
return cont.userID
}
func main() {
ctx1 := myType{"1"}
fmt.Println(ctx1.UserID()) // 1
ctx := myType{"2"}
var101 := ctx.UserID()
fmt.Println(ctx1.UserID(), var101) // 1 2
ctx2 := &myType{}
fmt.Println(ctx2) // &{}
var ctx3 *myType
fmt.Println(ctx3) // <nil>
}
Output:
1
1 2
&{}
<nil>
For Technique 1. I'm not sure what logging.Debug() does but I think you are trying to pass a string to it. In this case use ctx2.GetUserId() not ctx2.GetUserId. I know it's sounds silly but to call a function that takes no parameters you still need the brackets.
The major problem is that you are using the myType package but you think you are using the types package. Otherwise I think technique 2 would be OK.
And as Volker implied about tehcnique 3 you need to use & not * to take the address of an object.

Print an array of empty interfaces in golang?

I have a struct with a field of the form field []interface{}. If I print the field, I get back a pointer reference. If I try to dereference the field, I get an error "invalid indirect".
The code is below:
type MyType struct {
field []interface{}
}
myType := //create a MyType. Field is just an array of numbers
println(myType.field) // prints a pointer reference, ex: [1/1]0xc420269aa0
println(*(myType.field)) // doesn't compile
How do I print the values in myType.field?
The answer is to loop through the array, or even better, use fmt.Println.
func dump(items []interface{}) {
for i := 0; i < len(items); i++ {
fmt.Println(items[i])
}
}
// OR
fmt.Println(items)
another way you could use
https://play.golang.org/p/4lLQ4Gb2S0m
package main
import (
"encoding/json"
"fmt"
)
func main() {
fmt.Println(dump(new(bool)))
fmt.Println(dump([]interface{}{new(bool), new(string)}))
}
func dump(x interface{}) string {
json, err := json.MarshalIndent(x, "", " ")
if err != nil {
panic(err)
}
return string(json)
}
// Outputs:
// false
// [
// false,
// ""
// ]
Array interfaces can be printed as follows
questionSet := []interface{}{}
// ... code to fillup the question set
fmt.Printf("%t", questionSet)
and here is the result
[%!t(*domain.Question=&{First Economics question 3
[{Economics option 1 for student false}
{Economics option 2 for student false}
{Economics option 3 for student true}
{Economics option 4 for student false}]
[{Economics first topic} {Economics second topic}]})
%!t(*domain.Question=&{second Question 4
[{Qs2 Option 1 for student false}
{Qs2 Option 2 for student false}
{Qs2 Option 3 for student false}
{Qs2 Option 4 for student false}]
[{third topic} {fourth topic}]})]
The output here is not very pretty but at least you can see the result and extract your relevant information for further process.
In the above result domain.Question is a struct and here is the complete struct for your reference. Hope this will help you to understand better.
type Question struct {
Questions string `json:"questions,omitempty" bson:"questions"`
Marks string `json:"marks,omitempty" bson:"marks"`
Options []QuestionOptions `json:"options,omitempty" bson:"options"`
Topics []QuestionTopic `json:"topics,omitempty" bson:"topics"`
}
type QuestionOptions struct {
Option string `json:"option" bson:"option"`
Correct bool `json:"correct" bson:"correct"`
}
type QuestionTopic struct {
Topic string `json:"topic" bson:"topic"`
}
Consider that the verb %t is designed to print boolean (see the doc overview at https://golang.org/pkg/fmt/), as such, while using it to dump the content of a value works as of today, it is not the behavior intended by go authors, and as such, this behavior might change in the future.
Peace,

golang get the reflect.Type of a type

Is it possible and how to get the reflect.Type of a type without creating an object from the type and calling on it reflect.TypeOf(obj)
What in java will be: MyType.class
You can achieve this without an instantiation with the following syntax;
package main
import (
"fmt"
"reflect"
)
type Test struct {
}
func main() {
fmt.Println(reflect.TypeOf((*Test)(nil)).Elem())
}
play; https://play.golang.org/p/SkmBNt5Js6
Also, it's demonstrated in the reflect example here; https://golang.org/pkg/reflect/#example_TypeOf
No you can't have it directly, because in Go structs have no accessible fields to get their type.
One may think of tweaking it by doing the following:
type object struct {}
func main() {
var obj object
t := reflect.TypeOf(object)
fmt.Println(t)
// main.object
}
However, in Go every variable is initialized with its zero value, so this is perfectly equivalent to:
t := reflect.TypeOf(object{})
// main.object
If you look at Golang's source code, you'll see that reflect.Type is an interface implemented differently according to types, however you do not have access to those informations.
But, what you can do is get the type of a pointer to the struct and from there, get the actual type. The process is the same, except that a pointer's zero value is nil, so it takes less time to instantiate:
func main() {
ptr_t := reflect.TypeOf((*object)(nil))
fmt.Println(ptr_t)
// *main.object
t := ptr_t.Elem()
fmt.Println(t)
// main.object
}

Passing an struct to a Post martini routine

I have an issue using this statement
m.Post(Model, binding.Form(Wish), func(wish Wish, r render.Render, db *mgo.Database) {
This worked fine if I use the struct define inside the prog like
m.Post(Model, binding.Form(Wish1{}) , func(wish Wish1, r render.Render, db *mgo.Database) {
but I need this to be an independent package.
I get "Wish is not a type" wish is the return of the binding function.
This worked with a primary Type struct. I am passing the strut as a interface{}
I am using GO with Martini.Classic() It is really complicated for me to change Martini or Binding package. Any suggestions.
This is the all code
package chlistpkg
import (
"github.com/codegangsta/martini"
"github.com/codegangsta/martini-contrib/binding"
"github.com/codegangsta/martini-contrib/render"
"labix.org/v2/mgo"
"time"
"fmt"
"html/template"
"reflect"
"adminStruct"
)
just to show the struct that I need to pass as to routine Doall
type Wish1 struct {
Name string `form:"name"`
Description string `form:"description"`
AnyDate time.Time `form:"anydate"`
Active bool `form:"active"`
Number int `form:"number"`
NumDec float32 `form:"numDec"`
}
DB Returns a martini.Handler
func DB() martini.Handler {
session, err := mgo.Dial("mongodb://localhost")
if err != nil {
panic(err)
}
return func(c martini.Context) {
s := session.Clone()
c.Map(s.DB("advent2"))
defer s.Close()
c.Next()
}
}
GetAll returns all Wishes in the database
func GetAll(db *mgo.Database, entList interface{}) interface{} {
db.C("wishes").Find(nil).All(entList)
fmt.Println("GettAll entList =", entList)
return entList
}
func Doall(Model string, Wish interface{}, Wish2 interface{}, Wishlist interface{} ) {
m := martini.Classic()
fmt.Println ("martini.Classic =", m)
m.Use(martini.Static("images")) // serve from the "images" directory as well
m.Use(render.Renderer(render.Options{
Directory: "templates",
Layout: "layout",
}))
m.Use(DB())
m.Get(Model, func(r render.Render, db *mgo.Database) {
r.HTML(200, "lista4", GetAll(db, Wishlist))
})
binding does not take a pointer. I have to pass the struct by reference on "Wish"
the issue is the return on "wish Wish" I got an error Wish is not a type
at compilation time
m.Post(Model, binding.Form(Wish), func(wish Wish, r render.Render, db *mgo.Database) {
fmt.Println("Input wish =", wish)
db.C("wishes").Insert(wish)
r.HTML(200, "lista4", GetAll(db, Wishlist))
})
m.Run()
Thanks in advance
Luis
The reason you are getting an error is that you have called your type Wish1 (with a numerical 1) but you are referring to the Wish type (which does not exist!) in your code.
Change your struct to be:
// Note: "Wish", not "Wish1"
type Wish struct {
Name string `form:"name"`
Description string `form:"description"`
AnyDate time.Time `form:"anydate"`
Active bool `form:"active"`
Number int `form:"number"`
NumDec float32 `form:"numDec"`
}
If you want to put your type into another package (tip: don't overdo the sub-packages), then it will need to become a pkgname.Wish as names are fully qualified.
Added
After a second look, you're also messing things up here:
func Doall(Model string, Wish interface{}, Wish2 interface{}, Wishlist interface{} ) {
m := martini.Classic()
fmt.Println ("martini.Classic =", m)
m.Use(martini.Static("images")) // serve from the "images" directory as well
Your parameter list needs to provide a name for each type; you can't pass Wish interface{} as a parameter as Wish is a type, not a variable name.
You should either:
func DoAll(model string, wish interface{}, wish2 interface{}, wishList interface{}) { ... }
Or, better still, stop using interface{} like this and write:
func DoAll(model string, wishList []Wish, wishes... Wish) { ... }
However, your DoAll function does not seem to be referenced elsewhere, and is creating its own Martini instance. I highly suggest thinking about why things are "split out" like this if you're just starting out. Keep it simple - e.g.
func main() {
m := martini.Classic()
m.Use(martini.Static("images"))
m.Use(DB())
m.Use(render.Renderer(render.Options{...}))
// No need for an anonymous function, which just adds clutter
m.Get("/wishes/all", GetAllWishes)
// Same goes for here
m.Post("/wishes/new", PostWish)
m.Run()
}
PS: I've fixed the formatting of your code, as it has a lot of unnecessary spacing before/after parenthesis. Make sure to use gofmt, which is included with the Go install and can be hooked into most popular editors.

Resources