Decoding Go Objects: A Masterclass on How to Find the Type of an Object in Go - Strategies and Best Practices.

Decoding Go Objects: A Masterclass on How to Find the Type of an Object in Go - Strategies and Best Practices.


4 min read

The Essence of Object Types in Go

1. Static Typing in Go

Go is renowned for its static typing, where the type of a variable is determined at compile time. This brings clarity and performance benefits to the language. However, there are situations where dynamic typing or type discovery is necessary.

2. Reflection in Go

Reflection is a powerful concept in Go that allows you to inspect the type and structure of variables at runtime. The reflect package is a cornerstone of reflection in Go, providing tools for dynamic type discovery.

Basic Techniques for Type Discovery

1. Using fmt.Printf for Basic Inspection

var myVar int
fmt.Printf("Type: %T\n", myVar)

The %T verb in fmt.Printf prints the type of a variable. While simple, this technique is limited to basic types and may not be suitable for more complex scenarios.

2. Type Assertion

var myInterface interface{} = "Hello, Go!"

if str, ok := myInterface.(string); ok {
    fmt.Printf("Type Assertion Successful. Value: %s\n", str)
} else {
    fmt.Println("Type Assertion Failed")

Type assertion allows you to extract the underlying value of an interface and check its type. It's a powerful tool for working with interfaces.

3. Reflection for Type Discovery

var myVar float64
typeOfMyVar := reflect.TypeOf(myVar)
fmt.Printf("Type: %s\n", typeOfMyVar)

The reflect.TypeOf function provides a more sophisticated way to discover the type of a variable, especially in scenarios involving interfaces and complex types.

Digging Deeper into Reflection

1. Inspecting Fields of a Struct

type Person struct {
    Name string
    Age  int

p := Person{Name: "John Doe", Age: 30}
typeOfP := reflect.TypeOf(p)

for i := 0; i < typeOfP.NumField(); i++ {
    field := typeOfP.Field(i)
    fmt.Printf("Field Name: %s, Field Type: %s\n", field.Name, field.Type)

Reflection enables the inspection of struct fields, providing valuable insights into the structure of complex objects.

2. Examining Methods of a Struct

type Car struct {
    Model string

func (c Car) Start() {
    fmt.Println("Car is starting...")

func (c Car) Stop() {
    fmt.Println("Car is stopping...")

car := Car{Model: "Tesla"}
typeOfCar := reflect.TypeOf(car)

for i := 0; i < typeOfCar.NumMethod(); i++ {
    method := typeOfCar.Method(i)
    fmt.Printf("Method Name: %s, Method Type: %s\n", method.Name, method.Type)

Reflection extends its capabilities to inspecting methods associated with a struct, offering a comprehensive view of the object's behavior.

Practical Use Cases for Type Discovery

1. JSON Unmarshaling

type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`

jsonData := []byte(`{"name":"John Doe","age":30}`)
var person Person

if err := json.Unmarshal(jsonData, &person); err != nil {
    fmt.Println("Error:", err)
} else {
    typeOfPerson := reflect.TypeOf(person)
    fmt.Printf("Type of Person: %s\n", typeOfPerson)

When working with JSON data, dynamically discovering the type of the target struct is crucial for successful unmarshaling.

2. Database Operations with SQL

type Employee struct {
    ID   int
    Name string

rows, err := db.Query("SELECT * FROM employees")
if err != nil {
    fmt.Println("Error querying database:", err)

defer rows.Close()

for rows.Next() {
    var employee Employee
    if err := rows.Scan(&employee.ID, &employee.Name); err != nil {
        fmt.Println("Error scanning row:", err)

    typeOfEmployee := reflect.TypeOf(employee)
    fmt.Printf("Type of Employee: %s\n", typeOfEmployee)

In database operations, dynamically discovering the type of the scanned struct is valuable for the generic processing of query results.

Best Practices and Considerations

1. Handling Interfaces with reflect.Value

]var myVar interface{} = 42
value := reflect.ValueOf(myVar)

if value.Kind() == reflect.Int {
    intValue := value.Int()
    fmt.Printf("Integer Value: %d\n", intValue)
} else {
    fmt.Println("Not an integer")

Using reflect.Value allows for further examination of the underlying value and its kind.

2. Understanding the Limitations of Reflection

While reflection is a powerful tool, it comes with some trade-offs, such as potential performance overhead and limitations in handling unexported struct fields.


In this extensive guide, we've navigated the intricacies of finding the type of an object in Go. From basic techniques like fmt.Printf type assertion to the powerful realm of reflection, you now possess a diverse toolkit for type discovery. Practical use cases in scenarios like JSON unmarshaling and database operations illustrate the real-world applicability of these techniques. As you embark on your Go programming journey, mastering the art of type discovery will undoubtedly enhance your ability to write robust, dynamic, and efficient code.

More such articles:

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!