How to find the type of Struct in Golang?

05 May, 2020
A structure or struct in Golang is a user-defined data type which is a composition of various data fields. Each data field has its own data type, which can be a built-in or another user-defined type. Struct represents any real-world entity that has some set of properties/fields. Go does not support the concept of classes, structs are the only way to create a user-defined type in this language. There are various ways by which we can identify the type of struct in Go:
Method 1: Using reflect package
You can use the reflect package to find the given type of a struct. Reflection package allows determining the variables’ type at runtime.
Syntax:
func typeofstruct(x interface{}){
    fmt.Println(reflect.TypeOf(x))
}

or
func typeofstruct(x interface{}){
    fmt.Println(reflect.ValueOf(x).Kind())
}

Example:
package main  // importing required modulesimport (    "fmt"    "reflect")  //struct Student definitiontype Student struct {    name   string    rollno int    phone  int64    city   string}  func main() {      // making a struct instance    // note: data fields should be entered in the order    // they are declared in the struct definition    var st1 = Student{"Raman", 01, 88888888888, "Mumbai"}    fmt.Println(reflect.TypeOf(st1))    fmt.Println(reflect.ValueOf(st1).Kind())      // Naming fields while    // initializing a struct    st2 := Student{name: "Naman", rollno: 02,             phone: 1010101010, city: "Delhi"}    fmt.Println(reflect.TypeOf(st2))    fmt.Println(reflect.ValueOf(st2).Kind())}
Output:
main.Student
struct
main.Student
struct

Method reflect.TypeOf returns main.Student type while the reflect.Kind returns a struct. It is because the method reflect.TypeOf returns a variable of type reflect.Type. reflect.Type contains all the information about the type that defines the variable that was passed, in this case, Student. The kind tells what this type is made initially of- a pointer, an int, a string, a struct, an interface, or another built-in data type. In our case, the type is a Student, and the kind is a struct.
Method 2: Using type assertions
Another way to check a struct’s type can be by using a type switch and doing several type assertions. A type switch uses several type assertions in series and runs the first matching type. In this switch, the case contains the type which is going to compare with the type present in the switch expression, and if none of the cases matches, then the default case is evaluated.
Syntax:
switch optstatement; typeswitchexpression{
case typelist 1: Statement..
case typelist 2: Statement..
...
default: Statement..
}

Example:
// Golang program to find a struct type// using type assertionspackage main  import "fmt"  // struct Employee definitiontype Employee struct {    name        string    employee_id int}  func Teststruct(x interface{}) {    // type switch    switch x.(type) {    case Employee:        fmt.Println("Employee type")    case int:        fmt.Println("int type")    default:        fmt.Println("Error")    }}  func main() {    // Declaring and initializing a    // struct using a struct literal    t := Employee{"Ram", 1234}    Teststruct(t)}
Output:
Employee type

Picked
Go Language
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.