Learning to Use Go Reflection

Post 5 in a Series on Go

Jon Bodner
Dec 13, 2017 · 8 min read
Image for post
Image for post

What’s Reflection?

Image for post
Image for post

Finding Your Type

Type is  and kind is slice
Contained type:
Type is int and kind is int
Type is string and kind is string
Type is and kind is ptr
Contained type:
Type is string and kind is string
Type is Foo and kind is struct
Field 1 name is A type is int and kind is int
Tag is tag1:"First Tag" tag2:"Second Tag"
tag1 is First Tag tag2 is Second Tag
Field 2 name is B type is string and kind is string
Type is and kind is ptr
Contained type:
Type is Foo and kind is struct
Field 1 name is A type is int and kind is int
Tag is tag1:"First Tag" tag2:"Second Tag"
tag1 is First Tag tag2 is Second Tag
Field 2 name is B type is string and kind is string

Making a New Instance

hello
goodbye
{A:20 B:Greetings}, 20, Greetings

Making Without Make

[10]
map[hello:10]

Making Functions

starting
ending
calling main.timeMe took 1s
starting
ending
calling main.timeMeToo took 2s
4

I Want a New Struct

0
20

reflect me
[]
[1 2 3]

What Can’t You Do?

That’s Great, But What’s the Point?

Read Part #2 here.

Additional Links

Capital One Tech

The low down on our high tech from the engineering experts…

Jon Bodner

Written by

Distinguished Engineer at Capital One. Interested in programming languages, open source, technology trends, and the future of software engineering.

Capital One Tech

The low down on our high tech from the engineering experts at Capital One. Learn about the solutions, ideas and stories driving our tech transformation.

Jon Bodner

Written by

Distinguished Engineer at Capital One. Interested in programming languages, open source, technology trends, and the future of software engineering.

Capital One Tech

The low down on our high tech from the engineering experts at Capital One. Learn about the solutions, ideas and stories driving our tech transformation.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store