Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
How to find the type of Struct in Golang?
Next article icon

How to find the type of Struct in Golang?

Last Updated : 05 May, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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: C
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()) } 
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: C
// 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) } 
Output:
  Employee type  

Next Article
How to find the type of Struct in Golang?

H

Hritika Rajput
Improve
Article Tags :
  • Go Language
  • Write From Home

Similar Reads

    How to add a method to struct type in Golang?
    Structs consist of data, but apart from this, structs also tell about the behavior in the form of methods. Methods attached to structs is very much similar to the definition of normal functions, the only variation is that you need to additionally specify its type. A normal function returning an inte
    3 min read
    How to Find Length of Struct in Golang?
    In programming, data structures are vital in organizing and storing data efficiently. One such data structure in Go, also known as Golang, is the struct. This article will delve into the concept of structure in Golang and explore various methods to find its length.Table of ContentWhat is Struct with
    5 min read
    How to use Field Tags in the Definition of Struct Type in Golang?
    Golang provide structures for the definition of a custom datatype. The concept of structure in Go is similar to the structure in C/C++. Example: type Person struct { Name string Aadhaar int Street string HouseNo int } Structures in Golang can be written to files like JSON for storing data on a hard
    2 min read
    How to find the length of the pointer in Golang?
    Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Pointers in Golang are also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always
    2 min read
    How to use strconv.Itoa() Function in Golang?
    Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides an Itoa() function which is equivalent to FormatInt(int64(x), 10). Or in other words, Itoa() function returns the string representation of x
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences