How to find the type of Struct in Golang?

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

package main
  
// importing required modules
import (
    "fmt"
    "reflect"
)
  
//struct Student definition
type 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())
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to find a struct type
// using type assertions
package main
  
import "fmt"
  
// struct Employee definition
type 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)
}

chevron_right


Output:

Employee type



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.