Go Data Types

Go Tutorial

 Go Data Types – Complete Guide with Examples

In Go (Golang), data types define what kind of data a variable can store. Go is a statically typed language, which means every variable has a specific type known at compile time.

Understanding Go data types is essential for writing correct, efficient, and bug-free programs.


 What Are Data Types in Go?

A data type specifies:

  • The kind of value a variable can hold

  • How much memory it uses

  • What operations can be performed on it

Example:


 Categories of Data Types in Go

Go data types are mainly divided into:

  •  Basic Data Type
  •  Composite Data Type
  •  Reference Data Type

 1. Basic Data Type

 Integer Types

Used to store whole numbers.

TypeSize
intPlatform dependent
int88-bit
int1616-bit
int3232-bit
int6464-bit
uintUnsigned int
uint8Byte
uint16Unsigned 16-bit
uint32Unsigned 32-bit
uint64Unsigned 64-bit

Example:


 Floating-Point Type

Used for decimal numbers.

TypeDescription
float3232-bit decimal
float6464-bit decimal (default)

Boolean Type

Stores true or false.

  •  Only true or false allowed

 String Type

Stores text data.

  •  Strings are immutable
  • Use double quotes

 Rune Type

Represents a single Unicode character.

  • rune is an alias for int32.

2. Composite Data Types

 Array

Fixed-size collection of elements.

  •  Size is fixed
  •  Cannot grow

 Slice

Dynamic version of arrays.

  •  Flexible
  •  Most commonly used

 Struct

Used to group different data types.

  •  Custom data type
  •  Very powerful

 3. Reference Data Types

 Pointer

Stores memory address of a variable.

  •  Efficient
  •  Used to modify values

 Map

Stores key-value pairs.

  •  Fast lookup
  •  Reference type

Function Type

Functions are also data types.

  • Can be passed as arguments

 Special Data Types

interface{} (Empty Interface)

Can hold any type.

  •  Flexible
  •  Use carefully

 byte

Alias for uint8.

  •  Used for binary data

Type Inference in Go

Go automatically detects data type.

  •  Cleaner syntax

 Zero Values in Go

If a variable is declared without initialization, Go assigns a zero value.

TypeZero Value
int0
float640.0
boolfalse
string""
pointernil

Type Conversion (Not Automatic)

Go does not allow implicit type conversion.

 Invalid

Valid


 Common Mistakes

  •  Assuming automatic type conversion
  •  Using wrong data type
  •  Confusing array and slice
  •  Ignoring zero values
  • Overusing interface{}

 Best Practices

  •  Use correct data type
  •  Prefer slices over arrays
  • Avoid unnecessary type conversions
  • Use structs for related data
  • Keep types simple

 Interview Questions: Go Data Type

1. Is Go statically typed?
Yes.

2. Default float type in Go?
float64.

3. What is a rune?
Alias for int32.

4. Are strings mutable in Go?
No.

5. What is zero value?
Default value of a type.


 Summary

  •  Go has strong, static typing
  •  Data type define value behavior
  • Basic, composite, and reference types
  • Zero values avoid uninitialized bugs
  • Correct data type improve performance

Mastering Go data type is the foundation of clean, efficient, and professional Go programming

You may also like...