How to find the type of Struct in Golang?


In Golang, structs are a powerful and essential feature that helps in organizing data and improving code readability. A struct is a composite data type that groups together zero or more values of different types. It is a user-defined type that can be customized according to the programmer's needs. Sometimes, when working with large codebases, it can become difficult to keep track of the different types of structs being used. In this article, we will discuss how to find the type of struct in Golang.

Using the "reflect" Package

The "reflect" package in Golang provides a way to inspect types at runtime. We can use the "reflect.TypeOf()" function to determine the type of a struct. This function takes an interface{} as an argument and returns a Type object that represents the dynamic type of the value passed to it.

Example

Here's an example −

package main

import (
   "fmt"
   "reflect"
)

type Person struct {
   Name string
   Age  int
}

func main() {
   p := Person{Name: "John", Age: 30}
   t := reflect.TypeOf(p)
   fmt.Println(t)
}

Output

main.Person

Using the "%T" Format Specifier

Another way to find the type of a struct is to use the "%T" format specifier. This specifier can be used with the fmt.Printf() function to print the type of the value passed to it.

Example

Here's an example −

package main

import "fmt"
type Person struct {
   Name string
   Age  int
}

func main() {
   p := Person{Name: "John", Age: 30}
   fmt.Printf("Type of p: %T", p)
}

Output

Type of p: main.Person

Using The "go/types" Package

The "go/types" package in Golang provides a way to inspect types in a program statically. This package is particularly useful for analyzing Go code and finding the types of structs used in a program.

Example

Here's an example −

package main

import (
   "go/types"
   "golang.org/x/tools/go/packages"
   "fmt"
)

type Person struct {
   Name string
   Age  int
}

func main() {
   cfg := &packages.Config{Mode: packages.LoadAllSyntax}
   pkgs, _ := packages.Load(cfg, ".")
   for _, pkg := range pkgs {
      for _, file := range pkg.Syntax {
         for _, decl := range file.Decls {
            switch decl := decl.(type) {
               case *types.GenDecl:
                  for _, spec := range decl.Specs {
                     if ts, ok := spec.(*types.TypeSpec); ok {
                        if _, ok := ts.Type.(*types.Struct); ok {
                           fmt.Printf("Found struct %s\n", ts.Name())
                        }
                     }
                  }
            }
         }
      }
   }
}

This program uses the "go/types" package to find all the structs used in the current package. The program first loads all the Go source files in the package and then iterates over each file, looking for declarations that contain type specifications. If a type specification is found, the program checks if the type is a struct and prints its name.

Conclusion

In this article, we have discussed three different ways to find the type of a struct in Golang. The "reflect" package provides a way to inspect types at runtime, while the "%T" format specifier can be used to print the type of a value. The "go/types" package is particularly useful for analyzing Go code and finding the types of structs used in a program

Updated on: 05-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements