Skip to content

Instantly share code, notes, and snippets.

@drewolson
Last active March 6, 2026 11:34
Show Gist options
  • Select an option

  • Save drewolson/4771479 to your computer and use it in GitHub Desktop.

Select an option

Save drewolson/4771479 to your computer and use it in GitHub Desktop.
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

himekami commented Mar 4, 2014

Copy link
Copy Markdown

this is a good example for me.
thank you.

@asmedrano

Copy link
Copy Markdown

Thanks!

@doojin

doojin commented Jun 3, 2014

Copy link
Copy Markdown

Thank you very much!
Just helped me.

@lenw

lenw commented Aug 28, 2014

Copy link
Copy Markdown

@jameycribbs

Copy link
Copy Markdown

Thanks very much for this! Very helpful.

@0proto

0proto commented Aug 28, 2015

Copy link
Copy Markdown

Nice snippet, thanks!

@ulranh

ulranh commented Aug 28, 2015

Copy link
Copy Markdown

Thank you very much! Just needed it.

@froztyco

froztyco commented Sep 1, 2015

Copy link
Copy Markdown

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

@rogierlommers

Copy link
Copy Markdown

Thanks!

@sofyan-ahmad

Copy link
Copy Markdown

Thank you! 👍

@johnstevin

Copy link
Copy Markdown

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

@sdtsui

sdtsui commented Sep 22, 2016

Copy link
Copy Markdown

thanks @drewolson, and @lenw for posting the snippet

@rmasci

rmasci commented Feb 14, 2017

Copy link
Copy Markdown

yeah this helped me a lot!!

@maxymania

Copy link
Copy Markdown

This is a good example. Thanks.

@huzhengchuan

Copy link
Copy Markdown

good example. tks

@manigandand

Copy link
Copy Markdown

This is a good example. It helped me!

@oscarzhou

Copy link
Copy Markdown

So nice example. Hah, solve my problem perfectly

@Konstantin8105

Konstantin8105 commented Feb 21, 2018

Copy link
Copy Markdown

@babulal107

Copy link
Copy Markdown

This is a very good example of reflection. Thank you

@aorjoa

aorjoa commented Feb 28, 2020

Copy link
Copy Markdown

Thanks!!!

@TsuyoshiUshio

Copy link
Copy Markdown

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

@lemon-tree-sheng

Copy link
Copy Markdown

nice example!!!

@Lim79Plus

Copy link
Copy Markdown

Thank you! It's helpful for me!

@fishparmak

Copy link
Copy Markdown

Thank You :)

@maxim-ge

Copy link
Copy Markdown

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment