variables and data types in Go

Variables and Data Types in Go

March 25th, 2026
2583
6:00 Minutes

Golang, also called Go, is a motionless and strongly typed language. This means that every variable should have a related data type which can't be changed after declaration. Whereas variables in Go are announced through the 'var' keyword, further followed by the variable declarations through ':=' operator, where the kind is reasoned through the compiler. This blog will give a brief description on variables and data types in Go, what are variables in Go, what are data types in Go, types and more. Let's begin.

What are Variables in Go?

variables in go

A variable is a labelled storage location in memory made use to hold a value in Go. The value can be modified throughout the program's execution. Every variable in Go has a particular data type, dictating the type of values it can store. Types of values like texts, numbers, boolean values, and then the operations which can be done on it.

Enroll in igmGuru's Golang Training Program to build career in programming language

Types of Variables in Golang

Basically variables are the placeholders of the information that can be transformed at the running time. Variables in Go categorizes variables in different types according to the type of data they hold. Here are the types of variables in Go given below.

There are majorly 4 types of variables in Go.

1. Basic types

The basic types speak for the fundamental data values. Here are the basic types given below.

  • Boolean (bool)- They store 'true' or 'false'.
  • Numeric types- These are of three kinds:     
  1. Integers: They store whole numbers along with signed and unsigned variants and various bit sizes. 'int' and 'uint' are practically platform dependent in size.
  2. Floating-Point Numbers: They store numbers with decimal points. Like 'float32', 'float64'.
  3. Complex Numbers: They store numbers with real and imaginary parts, like 'complex64', 'complex128'.

  • String- They store sequences of characters surrounded in double quotes.

2. Aggregate Types

These types blend multiple values in a single entity. Here are the aggregate types given below. Here are the aggregate types given below.

  • Arrays- They are fixed-sized chains of elements of the same kind.
  • Structs- These are the collections of variables which can have multiple types, grouped together to show a single record.

3. Reference Types

These types of variables in Go hold the references to the fundamental data structures. Here are the reference types given below.

  • Pointers- They preserve the memory address of another variable.
  • Slices- These are large sized segments of arrays, offering a flexible way to work with sequence.
  • Maps- They are unordered collections of key value pairs, where keys are special and map to corresponding values.
  • Functions- These are the premium members in Go, as they can be assigned to variables and passed as arguments and returned from other tasks.
  • Channels- They give a way for goroutines for communicating with one another safely.

4. Interface Types

They explain a set of method signatures, permitting variables to hold values of any kind which apply to those methods.

Want to Master Golang Programming Language? 

Explore our Golang Tutorial to boost your coding skills and gain hands-on knowledge in Go Programming.

Explore Now
Golang Learning Illustration

What are Data Types in Go?

As a motionless typed programming language, Go differentiates data into multiple kinds, these represent the kind of values a variable can and the operations which can be done on them. Read on to know the types of data types in Go.

Types of Data Types in Go

data types in go

As data is a crucial concept in programming. It specifies the size and type of variable values. As it is a statically typed language, which means once a variable type is described, it can only preserve data of that kind. There are three basic data types in Go and they are-

  1. Bool- It shows a boolean value and it is either true or false.
  2. Numeric- It shows integer types, floating point values and complicated types.
  3. String- These show a string value.
  4. Derived - These are constructed or defined through one or more of the in-built basic data types. They give ways to organize and structure data in more meaningful ways.

Now, here is an example to make things easier for you to understand.

package main

import "fmt"

func main() {

// Integer

var num int = 25

// Float

var price float64 = 19.99

// String

var name string = "Go"

// Boolean

var isFun bool = true

// Slice (dynamic array)

var numbers []int = []int{1, 2, 3}

// Printing values and types

fmt.Println("num:", num, "(Type: int)")

fmt.Println("price:", price, "(Type: float64)")

fmt.Println("name:", name, "(Type: string)")

fmt.Println("isFun:", isFun, "(Type: bool)")

fmt.Println("numbers:", numbers, "(Type: slice)")

}package main

import "fmt"

func main() {

// Integer

var num int = 25

// Float

var price float64 = 19.99

// String

var name string = "Go"

// Boolean

var isFun bool = true

// Slice (dynamic array)

var numbers []int = []int{1, 2, 3}

// Printing values and types

fmt.Println("num:", num, "(Type: int)")

fmt.Println("price:", price, "(Type: float64)")

fmt.Println("name:", name, "(Type: string)")

fmt.Println("isFun:", isFun, "(Type: bool)")

fmt.Println("numbers:", numbers, "(Type: slice)")

}

Here is the output of the code given above, when you run the program:

num: 25 (Type: int)

price: 19.99 (Type: float64)

name: Go (Type: string)

isFun: true (Type: bool)

numbers: [1 2 3] (Type: slice)

Now, here is an explanation on the example given above:

The Integer (int) stores whole numbers (e.g., 25). Then Float (float64) stores decimal numbers (example 19.99). The String stores text (e.g 'Go'). Then the Boolean (bool) variable stores true or false. And Slice ([]int) is a flexible array which can grow for instance, [1, 2, 3].

This example keeps it simple by focusing on commonly used data types, showing their values and types in a clear and beginner-friendly way.

Read Also- Top 50+ Golang Interview Questions And Answers

How to Find Variable Types in Go?

As we know variables are a placeholder of the information that can be changed during runtime. They permit to Retrieve and Manipulate the stored information. There are three ways to find variable types in Go and they are -

  1. Using reflect.TypeOf Function
  2. Through making use of the reflect.ValueOf.Kind() Function.
  3. Through using %T with Printf.

Here is an example given below to make things easier for you to understand.

package main

import (

"fmt"

"reflect"

)

func main() {

num := 42

price := 9.99

name := "Go"

// Method 1: Using fmt.Printf with %T

fmt.Printf("num type: %T\n", num)

fmt.Printf("price type: %T\n", price)

fmt.Printf("name type: %T\n", name)

// Method 2: Using reflect.TypeOf

fmt.Println("num type:", reflect.TypeOf(num))

fmt.Println("price type:", reflect.TypeOf(price))

fmt.Println("name type:", reflect.TypeOf(name))

}

Now, take a look at the output of the example given below.

num type: int

price type: float64

name type: string

num type: int

price type: float64

name type: string

Here is an explanation on the example given above -

It is making use of fmt.Printf with %T. Here, the %T format verb in fmt.Printf directly prints the type of a variable like int, float64, string.

Secondly it makes use of reflect.TypeOf. The reflect package's TypeOf function returns the type of a variable and is useful for programmatic type checking.

Wrapping Up

We read in this blog about the variables and data types in Go with examples to understand these in an easier way. These data types define the type of data that a valid Go variable can hold.

FAQs: Variables and Data Types in Go

Q1. How do I get to know the datatype of a variable?

You can make use of typeof to check the data structure or type of a variable. It's useful in debugging while working with various data types.

Q2. What's an interface in Go?

It describes the exact methods that some other type should have.

Q3. Where are the Go env stored?

If your GOPATH environment variable is not set then the default location is $HOME/go on Linux and macOS and %USERPROFILE%\go on Windows.

Q4. Why should freshers learn Go data types?

Understanding Go data types helps freshers store and use data correctly. It also improves code efficiency and reduces errors in programs.

Course Schedule

Course NameBatch TypeDetails
Golang Training
Every WeekdayView Details
Golang Training
Every WeekendView Details
About the Author
Piyush Verma | igmGuru
About the Author

Piyush is a technical writer skilled in Golang, R, C, C#, C++, Ruby, and ERP systems. He simplifies complex coding concepts into clear, beginner-friendly content, helping readers build strong foundations. With a structured approach, he supports both beginners and professionals in mastering technologies and advancing their careers.

Drop Us a Query
Fields marked * are mandatory

Programming Certification Courses

×

Your Shopping Cart


Your shopping cart is empty.