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:
Different Ways to Find the Type of Variable in Golang
Next article icon

Different Ways to Find the Type of Variable in Golang

Last Updated : 17 May, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Variable is a placeholder of the information which can be changed at runtime. And variables allow to Retrieve and Manipulate the stored information. There are 3 ways to find the type of variables in Golang as follows:
  • Using reflect.TypeOf Function
  • Using reflect.ValueOf.Kind() Function
  • Using %T with Printf
Example: C
// Golang program to show the different ways // to find the Type of a Variable package main  // import the fmt and reflect package import (     "fmt"     "reflect" )  //main function  func main() {      // string type     var1 := "hello world"      // integer     var2 := 10      // float     var3 := 1.55      // boolean     var4 := true      // shorthand string array declaration     var5 := []string{"foo", "bar", "baz"}      // map is reference datatype     var6 := map[int]string{100: "Ana", 101: "Lisa", 102: "Rob"}      // complex64 and complex128     // is basic datatype     var7 := complex(9, 15)      // using %T format specifier to     // determine the datatype of the variables      fmt.Println("Using Percent T with Printf")     fmt.Println()      fmt.Printf("var1 = %T\n", var1)     fmt.Printf("var2 = %T\n", var2)     fmt.Printf("var3 = %T\n", var3)     fmt.Printf("var4 = %T\n", var4)     fmt.Printf("var5 = %T\n", var5)     fmt.Printf("var6 = %T\n", var6)     fmt.Printf("var7 = %T\n", var7)      // using TypeOf() method of reflect package     // to determine the datatype of the variables     fmt.Println()     fmt.Println("Using reflect.TypeOf Function")     fmt.Println()      fmt.Println("var1 = ", reflect.TypeOf(var1))     fmt.Println("var2 = ", reflect.TypeOf(var2))     fmt.Println("var3 = ", reflect.TypeOf(var3))     fmt.Println("var4 = ", reflect.TypeOf(var4))     fmt.Println("var5 = ", reflect.TypeOf(var5))     fmt.Println("var6 = ", reflect.TypeOf(var6))     fmt.Println("var7 = ", reflect.TypeOf(var7))      // using ValueOf() method of reflect package     // to determine the value of the variable     // Kind() method returns the datatype of the     // value fetched by the ValueOf() method     fmt.Println()     fmt.Println("Using reflect.ValueOf.Kind() Function")     fmt.Println()      fmt.Println("var1 = ", reflect.ValueOf(var1).Kind())     fmt.Println("var2 = ", reflect.ValueOf(var2).Kind())     fmt.Println("var3 = ", reflect.ValueOf(var3).Kind())     fmt.Println("var4 = ", reflect.ValueOf(var4).Kind())     fmt.Println("var5 = ", reflect.ValueOf(var5).Kind())     fmt.Println("var6 = ", reflect.ValueOf(var6).Kind())     fmt.Println("var7 = ", reflect.ValueOf(var7).Kind())  } 
Output:
  Using Percent T with Printf    var1 = string  var2 = int  var3 = float64  var4 = bool  var5 = []string  var6 = map[int]string  var7 = complex128    Using reflect.TypeOf Function    var1 =  string  var2 =  int  var3 =  float64  var4 =  bool  var5 =  []string  var6 =  map[int]string  var7 =  complex128    Using reflect.ValueOf.Kind() Function    var1 =  string  var2 =  int  var3 =  float64  var4 =  bool  var5 =  slice  var6 =  map  var7 =  complex128  

Next Article
Different Ways to Find the Type of Variable in Golang

S

Shreyasi_Chakraborty
Improve
Article Tags :
  • Go Language

Similar Reads

    Different Ways to Find the Type of an Object in Golang
    Go does not have the concept of a class type, therefore, you do not have objects in Go. You can refer any data type in Go as an object. There are several data types provided by Go such as int8, int16, int32, int64, float64, string, bool etc. There are three different ways by which you can find the t
    4 min read
    Different Ways to Convert an Integer Variable to String in Golang
    Integer variable cannot be directly convert into String variable. In order to convert string to integer type in Golang , you have store value of integer variable as string in string variable. For this, we are using strconv and fmt package functions. 1. Itoa() Function:  The Itoa stands for Integer t
    3 min read
    Different Ways to Convert the Boolean Type in String in Golang
    In order to convert Boolean Type to String type in Golang , you can use the strconv and fmt package function. 1. strconv.FormatBool() Method: The FormatBool is used to Boolean Type to String. It returns "true" or "false" according to the value of b. Syntax: func FormatBool(b bool) string Example : C
    2 min read
    Golang program that uses func with variable argument list
    In Golang, a function that can be called with a variable argument list is known as a variadic function. One can pass zero or more arguments in the variadic function. If the last parameter of a function definition is prefixed by ellipsis ..., then the function can accept any number of arguments for t
    2 min read
    How to Check the Data Type of a Variable in Ruby?
    In this article, we will discuss how to check the data type of a variable in Ruby. We can check the data types of a variable through different methods ranging from class to kind_of method in Ruby. Table of Content Using Class method Using is_a? Method Using kind_of? MethodUsing Class method The Clas
    3 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