Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Golang Reflection Example
package main
import (
"fmt"
"reflect"
)
type Foo struct {
FirstName string `tag_name:"tag 1"`
LastName string `tag_name:"tag 2"`
Age int `tag_name:"tag 3"`
}
func (f *Foo) reflect() {
val := reflect.ValueOf(f).Elem()
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
tag := typeField.Tag
fmt.Printf("Field Name: %s,\t Field Value: %v,\t Tag Value: %s\n", typeField.Name, valueField.Interface(), tag.Get("tag_name"))
}
}
func main() {
f := &Foo{
FirstName: "Drew",
LastName: "Olson",
Age: 30,
}
f.reflect()
}
@himekami

This comment has been minimized.

Copy link

himekami commented Mar 4, 2014

this is a good example for me.
thank you.

@asmedrano

This comment has been minimized.

Copy link

asmedrano commented Apr 10, 2014

Thanks!

@doojin

This comment has been minimized.

Copy link

doojin commented Jun 3, 2014

Thank you very much!
Just helped me.

@lenw

This comment has been minimized.

Copy link

lenw commented Aug 28, 2014

@jameycribbs

This comment has been minimized.

Copy link

jameycribbs commented Apr 3, 2015

Thanks very much for this! Very helpful.

@0proto

This comment has been minimized.

Copy link

0proto commented Aug 28, 2015

Nice snippet, thanks!

@ulranh

This comment has been minimized.

Copy link

ulranh commented Aug 28, 2015

Thank you very much! Just needed it.

@danmondy

This comment has been minimized.

Copy link

danmondy commented Sep 1, 2015

Does the reflection logic change if foo is passed in as an empty interface? interface{}?

@rogierlommers

This comment has been minimized.

Copy link

rogierlommers commented Jan 14, 2016

Thanks!

@sofyan-ahmad

This comment has been minimized.

Copy link

sofyan-ahmad commented Apr 23, 2016

Thank you! 👍

@johnstevin

This comment has been minimized.

Copy link

johnstevin commented May 6, 2016

很好的例子,感谢作者!
thanks to the author,A good example!

@sdtsui

This comment has been minimized.

Copy link

sdtsui commented Sep 22, 2016

thanks @drewolson, and @lenw for posting the snippet

@rmasci

This comment has been minimized.

Copy link

rmasci commented Feb 14, 2017

yeah this helped me a lot!!

@maxymania

This comment has been minimized.

Copy link

maxymania commented Sep 21, 2017

This is a good example. Thanks.

@huzhengchuan

This comment has been minimized.

Copy link

huzhengchuan commented Nov 14, 2017

good example. tks

@manigandand

This comment has been minimized.

Copy link

manigandand commented Jan 4, 2018

This is a good example. It helped me!

@OscarZhou

This comment has been minimized.

Copy link

OscarZhou commented Jan 25, 2018

So nice example. Hah, solve my problem perfectly

@Konstantin8105

This comment has been minimized.

Copy link

Konstantin8105 commented Feb 21, 2018

@babulal107

This comment has been minimized.

Copy link

babulal107 commented Jun 20, 2019

This is a very good example of reflection. Thank you

@Aorjoa

This comment has been minimized.

Copy link

Aorjoa commented Feb 28, 2020

Thanks!!!

@TsuyoshiUshio

This comment has been minimized.

Copy link

TsuyoshiUshio commented Mar 2, 2020

It is what exactly I wanted. :) It helps to save my time a lot. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.