...

/

Learning the Internal Structure of a Go Structure

Learning the Internal Structure of a Go Structure

Learn learn how to use reflection in Go.

We'll cover the following...

Coding example

The next utility, reflection.go, shows us how to use reflection to discover the internal structure and fields of a Go structure variable.

Press + to interact
package main
import (
"fmt"
"reflect"
)
type Secret struct {
Username string
Password string
}
type Record struct {
Field1 string
Field2 float64
Field3 Secret
}

In the code above, we define two struct types: Secret and Record. The Secret struct has two fields: Username and Password, both of which are of type string. This struct can be used to store sensitive information such as user credentials.

The Record struct has three fields: Field1, Field2, and Field3. Field1 is of type string, Field2 ...