{"id":435,"date":"2026-05-28T09:00:00","date_gmt":"2026-05-28T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/"},"modified":"2026-05-28T09:00:00","modified_gmt":"2026-05-28T03:30:00","slug":"go-reflection-basics","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/","title":{"rendered":"Go Reflection Explained with Simple Examples"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go reflection lets a program look at its own types and values while it runs. In this post, you will learn what the <code>reflect<\/code> package does. You will also see how to read struct fields and tags and how to change values safely. Then we will build a tiny validator, which is how many real libraries work inside.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This lesson builds on <a href=\"https:\/\/smitabharat.com\/blog\/go-structs-beginners-guide\/\">Go structs explained<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-interfaces-examples\/\">Go interfaces with examples<\/a>. We also use the ideas from <a href=\"https:\/\/smitabharat.com\/blog\/go-type-assertion-type-switch\/\">Go type assertion and type switch<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-input-validation\/\">Go input validation<\/a>. So it helps to know those first, because reflection builds on them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is Go Reflection?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In short, reflection means a program can inspect and change its own structure at run time. Normally, the compiler knows every type before the program starts. With reflection, however, your code can receive a value of any type. Then it can ask, &#8220;What are you, and what is inside you?&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of an X-ray machine at a hospital. A doctor does not open the patient to see the bones. Instead, the machine shows what is inside. Similarly, reflection shows the fields, tags and methods of a value without knowing its type in advance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>reflect<\/code> package has two main types. First, <code>reflect.Type<\/code> describes a type, such as its name and its fields. Second, <code>reflect.Value<\/code> holds the actual data and lets you read or change it. Together, they power <code>encoding\/json<\/code>, <code>fmt<\/code> and many other packages. For example, <code>json.Marshal<\/code> uses them to find the field names of your struct.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Reflection Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Most of the time you do not need reflection. However, it is the secret behind many tools you use every day. So here is why Go reflection is worth learning.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>It explains the libraries<\/strong>: JSON, ORMs and validators all use it.<\/li>\n\n\n\n<li><strong>Generic helpers<\/strong>: you can write one function that works on any struct.<\/li>\n\n\n\n<li><strong>Struct tags<\/strong>: you can read tags such as <code>json:\"name\"<\/code> and act on them.<\/li>\n\n\n\n<li><strong>Debugging tools<\/strong>: you can print or compare values of unknown types.<\/li>\n\n\n\n<li><strong>Better judgement<\/strong>: you learn when reflection is the right tool, and when it is not.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Because of this, even beginners benefit from a short tour of the package. In fact, once you see how a validator reads tags, many library errors start to make sense. Therefore, this lesson is a good investment even if you rarely write reflective code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Reflection Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Every Go value stored in an interface carries two hidden facts, which are its type and its data. The <code>reflect<\/code> package lets you read both, which is the core of Go reflection. In Go reflection, you first call <code>reflect.TypeOf(x)<\/code>, which returns the type. Next, <code>reflect.ValueOf(x)<\/code> returns the value. Then you can walk through fields, check the kind and read or set data.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-1-1024x640.png\" alt=\"Go Reflection diagram showing TypeOf and ValueOf feeding a tag validator\" class=\"wp-image-424\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">A value goes into reflect.TypeOf and reflect.ValueOf. TypeOf describes names, kinds and tags, and ValueOf gives access to the data for reading and setting.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Notice that reflection has a special word called kind. The kind is the basic category, such as <code>int<\/code>, <code>string<\/code>, <code>slice<\/code>, <code>map<\/code> or <code>struct<\/code>. For example, <code>type Celsius float64<\/code> has the type <code>Celsius<\/code> but the kind <code>float64<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Most Used Functions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You only need a handful of functions at first, so the table below lists them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Function<\/th><th>What it does<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><code>reflect.TypeOf(x)<\/code><\/td><td>Returns the type of x<\/td><td><code>User<\/code><\/td><\/tr><tr><td><code>reflect.ValueOf(x)<\/code><\/td><td>Returns the value of x<\/td><td>The data inside<\/td><\/tr><tr><td><code>t.Kind()<\/code><\/td><td>Returns the category<\/td><td><code>struct<\/code>, <code>int<\/code><\/td><\/tr><tr><td><code>t.NumField()<\/code> and <code>t.Field(i)<\/code><\/td><td>Lists struct fields<\/td><td>Name, Email<\/td><\/tr><tr><td><code>f.Tag.Get(\"json\")<\/code><\/td><td>Reads a struct tag<\/td><td><code>\"name\"<\/code><\/td><\/tr><tr><td><code>v.Elem().Field(i).SetString(...)<\/code><\/td><td>Changes a field<\/td><td>New name<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">The Three Laws in Plain Words<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Go team describes reflection with three laws. First, reflection goes from an interface value to a reflection object. Second, it goes back from a reflection object to an interface value, using <code>Interface()<\/code>. Third, a reflection value must be settable before you can change it. In practice, that means you must pass a pointer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go Reflection vs Type Switches and Generics<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Reflection is only one way to handle many types. For that reason, the table below compares three options.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Reflection<\/th><th>Type switch<\/th><th>Generics<\/th><\/tr><\/thead><tbody><tr><td>When types are checked<\/td><td>Run time<\/td><td>Run time<\/td><td>Compile time<\/td><\/tr><tr><td>Works for unknown types<\/td><td>Yes<\/td><td>Only listed types<\/td><td>Only matching constraints<\/td><\/tr><tr><td>Speed<\/td><td>Slowest<\/td><td>Fast<\/td><td>Fast<\/td><\/tr><tr><td>Type safety<\/td><td>Low, errors are panics<\/td><td>Medium<\/td><td>High<\/td><\/tr><tr><td>Best for<\/td><td>Tags and unknown structs<\/td><td>A few known types<\/td><td>Reusable typed code<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-2-1024x640.png\" alt=\"Go Reflection diagram comparing reflection, type switches and generics\" class=\"wp-image-425\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Reflection reads types at run time and works with any struct, while generics are checked by the compiler and a type switch handles a short list of known types.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So when should you use which? Prefer generics or a type switch whenever you can. On the other hand, use reflection when you must read struct tags or handle types you cannot list in advance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go Reflection Hands-On: Inspect and Validate a Struct<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s try the package for real. We use a <code>User<\/code> struct with tags. Also, every output below comes from a real run on Go 1.24.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Inspect Any Struct<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, here is the struct and a function that prints the type, the kind and every field. Because it accepts <code>any<\/code>, you can pass any struct.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type User struct {\n\tName  string `json:\"name\" validate:\"required\"`\n\tEmail string `json:\"email\" validate:\"required\"`\n\tAge   int    `json:\"age\" validate:\"min=18\"`\n\ttoken string \/\/ unexported\n}\n\nfunc inspect(v any) {\n\tt := reflect.TypeOf(v)\n\tval := reflect.ValueOf(v)\n\tfmt.Println(\"type:\", t.Name(), \"kind:\", t.Kind())\n\tfor i := 0; i &lt; t.NumField(); i++ {\n\t\tf := t.Field(i)\n\t\tfmt.Printf(\"  %-6s %-7s json=%q exported=%v\\n\", f.Name, f.Type, f.Tag.Get(\"json\"), f.IsExported())\n\t\tif f.IsExported() {\n\t\t\tfmt.Printf(\"         value=%v\\n\", val.Field(i).Interface())\n\t\t}\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When we call <code>inspect(User{Name: \"Asha\", Email: \"\", Age: 15})<\/code>, we get this output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type: User kind: struct\n  Name   string  json=\"name\" exported=true\n         value=Asha\n  Email  string  json=\"email\" exported=true\n         value=\n  Age    int     json=\"age\" exported=true\n         value=15\n  token  string  json=\"\" exported=false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Notice that the loop skips the value of <code>token<\/code>. Reading the value of an unexported field with <code>Interface()<\/code> would panic. Therefore, the code checks <code>IsExported()<\/code> first.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Build a Tiny Validator from Tags<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we read the <code>validate<\/code> tag of each field and apply simple rules. This is a small version of what real validation libraries do. As a result, you can see the idea behind them in about twenty lines.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func validate(v any) []string {\n\tvar problems []string\n\tval := reflect.ValueOf(v)\n\tt := val.Type()\n\tfor i := 0; i &lt; t.NumField(); i++ {\n\t\trule := t.Field(i).Tag.Get(\"validate\")\n\t\tfv := val.Field(i)\n\t\tswitch {\n\t\tcase rule == \"required\" &amp;&amp; fv.IsZero():\n\t\t\tproblems = append(problems, t.Field(i).Name+\" is required\")\n\t\tcase strings.HasPrefix(rule, \"min=\"):\n\t\t\tmin, _ := strconv.Atoi(strings.TrimPrefix(rule, \"min=\"))\n\t\t\tif fv.Int() &lt; int64(min) {\n\t\t\t\tproblems = append(problems, fmt.Sprintf(\"%s must be at least %d\", t.Field(i).Name, min))\n\t\t\t}\n\t\t}\n\t}\n\treturn problems\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Running <code>validate(u)<\/code> on our user prints the two problems.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>problems: [Email is required Age must be at least 18]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>IsZero<\/code> method checks for the zero value, such as an empty string. Also, <code>fv.Int()<\/code> reads any integer kind as an <code>int64<\/code>. As a result, the same validator works on structs with many field types. Compare this with the hand-written <code>Validate()<\/code> method from the input validation lesson. Reflection saves typing, but plain code is easier to debug.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Change a Value with a Pointer<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s set a field. First, we must pass a pointer. Then we call <code>Elem()<\/code> to reach the struct itself.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pv := reflect.ValueOf(&amp;u).Elem()\npv.FieldByName(\"Name\").SetString(\"Asha Mehta\")\nfmt.Println(\"after set:\", u.Name)\nfmt.Println(\"can set token?\", pv.FieldByName(\"token\").CanSet())<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output shows that exported fields can change. In contrast, unexported fields cannot.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>after set: Asha Mehta\ncan set token? false<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Call a Method by Name<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, reflection can also call methods. In fact, this is how some frameworks find handlers by name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (u User) Greet(greeting string) string { return greeting + \", \" + u.Name }\n\nm := reflect.ValueOf(u).MethodByName(\"Greet\")\nout := m.Call([]reflect.Value{reflect.ValueOf(\"Hello\")})\nfmt.Println(\"method result:\", out[0])<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The result is <code>Hello, Asha Mehta<\/code>. Also, only exported methods can be found this way.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Check the Kind of Any Value<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A loop with <code>Kind()<\/code> can describe unknown values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for _, x := range []any{42, \"go\", 3.14, []int{1, 2}, map[string]int{\"a\": 1}} {\n\tfmt.Printf(\"%v -&gt; %s\\n\", x, reflect.TypeOf(x).Kind())\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output lists the kind for each value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>42 -&gt; int\ngo -&gt; string\n3.14 -&gt; float64\n[1 2] -&gt; slice\nmap[a:1] -&gt; map<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Measure the Cost<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Reflection is slower than normal code. To measure it, this benchmark reads a field directly and through reflection.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BenchmarkDirect-2    \t1000000000\t         0.5855 ns\/op\t       0 B\/op\t       0 allocs\/op\nBenchmarkReflect-2   \t23005450\t        54.78 ns\/op\t       0 B\/op\t       0 allocs\/op<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On our machine, the reflection call took about 55 nanoseconds, while the direct read took less than one. That is roughly 90 times slower, but still tiny for a single call. However, it matters inside hot loops. So cache what you can, and avoid reflection in code that runs millions of times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Go Reflection<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Using reflection when a type switch is enough<\/strong>: the code becomes harder to read. So try simple tools first, and add Go reflection only when you must.<\/li>\n\n\n\n<li><strong>Setting a value without a pointer<\/strong>: <code>reflect.ValueOf(u)<\/code> is a copy and cannot be set. Instead, pass <code>&amp;u<\/code> and call <code>Elem()<\/code>.<\/li>\n\n\n\n<li><strong>Touching unexported fields<\/strong>: <code>Interface()<\/code> and <code>Set...<\/code> panic on them. Therefore, check <code>IsExported()<\/code> and <code>CanSet()<\/code> first.<\/li>\n\n\n\n<li><strong>Ignoring panics<\/strong>: reflection reports many mistakes as panics. So test your reflective code well.<\/li>\n\n\n\n<li><strong>Using it in hot paths<\/strong>: reflection costs real time. Instead, cache the type information once and reuse it.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Reflection<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Prefer generics and interfaces<\/strong>, because the compiler then checks your code.<\/li>\n\n\n\n<li><strong>Keep reflection in small helper functions<\/strong>, so the rest of the code stays simple.<\/li>\n\n\n\n<li><strong>Check the kind before reading<\/strong>, since calling <code>Int()<\/code> on a string panics.<\/li>\n\n\n\n<li><strong>Cache field lookups<\/strong>, which saves time when you process many values.<\/li>\n\n\n\n<li><strong>Write table-driven tests<\/strong>, because reflective code has many edge cases.<\/li>\n\n\n\n<li><strong>Document the tags you support<\/strong>, so other developers know the rules. Clear notes make Go reflection code much easier to maintain.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To practise Go reflection, extend the validator with a <code>max=<\/code> rule for integers and a <code>maxlen=<\/code> rule for strings. Then add a field <code>Bio string<\/code> with <code>validate:\"maxlen=100\"<\/code>. Finally, test it with a long text.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hint: use <code>fv.Kind() == reflect.String<\/code> before calling <code>fv.Len()<\/code>. After that, write a table test with one valid user and three invalid ones.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These pages describe the package and the three laws in detail.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/reflect\">reflect package documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/go.dev\/blog\/laws-of-reflection\">The Laws of Reflection<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/go.dev\/ref\/spec#Struct_types\">Go spec: struct tags<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go reflection lets code inspect types and values at run time.<\/li>\n\n\n\n<li><code>reflect.TypeOf<\/code> describes the type, and <code>reflect.ValueOf<\/code> holds the data.<\/li>\n\n\n\n<li>The kind tells you the basic category, such as struct or slice.<\/li>\n\n\n\n<li>Pass a pointer and call <code>Elem()<\/code> when you need to change a value.<\/li>\n\n\n\n<li>Struct tags let libraries add rules and names to fields.<\/li>\n\n\n\n<li>Reflection is slower and less safe, so use it only when needed.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By now, you know how many Go libraries work inside. However, real products also need bigger designs. So next, we will look at splitting one app into small services.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coming soon: Go Microservices Basics<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Go reflection lets a program look at its own types and values while it runs. In this post, you will learn what the reflect&hellip;<\/p>\n","protected":false},"author":1,"featured_media":423,"comment_status":"","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[15],"tags":[80,11,84],"swapz_series":[],"class_list":["post-435","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-lang","tag-advanced","tag-go-lang","tag-go-reflection"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Reflection Explained with Simple Examples<\/title>\n<meta name=\"description\" content=\"Learn Go reflection step by step: read types, values and struct tags, set fields safely, build a tag validator and know its cost.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Reflection Explained with Simple Examples\" \/>\n<meta property=\"og:description\" content=\"Learn Go reflection step by step: read types, values and struct tags, set fields safely, build a tag validator and know its cost.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-05-28T03:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Reflection Explained with Simple Examples\",\"datePublished\":\"2026-05-28T03:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/\"},\"wordCount\":1473,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-reflection-basics-featured.png\",\"keywords\":[\"Advanced\",\"Go Lang\",\"Go Reflection\"],\"articleSection\":[\"Go Lang\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/\",\"name\":\"Go Reflection Explained with Simple Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-reflection-basics-featured.png\",\"datePublished\":\"2026-05-28T03:30:00+00:00\",\"description\":\"Learn Go reflection step by step: read types, values and struct tags, set fields safely, build a tag validator and know its cost.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-reflection-basics-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-reflection-basics-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Reflection featured image: TypeOf, ValueOf and struct tags\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-reflection-basics\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Reflection Explained with Simple Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Reflection Explained with Simple Examples","description":"Learn Go reflection step by step: read types, values and struct tags, set fields safely, build a tag validator and know its cost.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/","og_locale":"en_US","og_type":"article","og_title":"Go Reflection Explained with Simple Examples","og_description":"Learn Go reflection step by step: read types, values and struct tags, set fields safely, build a tag validator and know its cost.","og_url":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-05-28T03:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Reflection Explained with Simple Examples","datePublished":"2026-05-28T03:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/"},"wordCount":1473,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-featured.png","keywords":["Advanced","Go Lang","Go Reflection"],"articleSection":["Go Lang"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/","url":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/","name":"Go Reflection Explained with Simple Examples","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-featured.png","datePublished":"2026-05-28T03:30:00+00:00","description":"Learn Go reflection step by step: read types, values and struct tags, set fields safely, build a tag validator and know its cost.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-reflection-basics\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-reflection-basics-featured.png","width":2400,"height":1260,"caption":"Go Reflection featured image: TypeOf, ValueOf and struct tags"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Reflection Explained with Simple Examples"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=435"}],"version-history":[{"count":0,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/435\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/423"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=435"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}