Golang Data types

Jyothi
4 min readFeb 15, 2025

--

In Go, data types are an important concept, as they define the type of data a variable can hold. Go is a statically typed language, meaning that the type of variables is known at compile time. Each variable must be assigned a specific data type, which determines the size and layout of the variable’s memory, the range of values it can hold, and the set of operations that can be performed on it.

Here are the main categories of data types in Go:

1. Basic Types

Go has a few fundamental types that are built into the language. These include numeric types, booleans, and strings.

Numeric Types

Numeric types are used to store numbers, and Go offers several numeric data types, including integers, floating-point numbers, and complex numbers.

  • Integers
  • Signed Integers: int, int8, int16, int32, int64
  • Unsigned Integers: uint, uint8, uint16, uint32, uint64
  • Examples:
  • var a int = 42 // 32 or 64-bit integer (depending on platform)
  • var b int8 = -128 // 8-bit signed integer (range: -128 to 127)
  • var c uint16 = 65535 // 16-bit unsigned integer (range: 0 to 65535)
  • Floating-Point Numbers
  • float32: 32-bit floating-point number
  • float64: 64-bit floating-point number (double precision)
  • Examples:
var x float32 = 3.14
var y float64 = 2.718
  • Complex Numbers
  • complex64: Complex number with float32 real and imaginary parts
  • complex128: Complex number with float64 real and imaginary parts
  • Examples:
var z complex64 = 1 + 2i  // A complex number with real = 1 and imaginary = 2

Boolean

The boolean type in Go is represented by the keyword bool. It can only hold one of two values: true or false.

Example:

var isAvailable bool = true

String

A string is a sequence of characters. Strings are immutable, meaning they cannot be changed once created.

Example:

var name string = "Go Programming"
  • You can concatenate strings using the + operator:
  • var fullName = "John" + " " + "Doe"

2. Derived/Composite Types

Go provides several composite types that are used to build more complex data structures.

Arrays

An array is a fixed-length sequence of elements of a particular data type. Once the size of an array is defined, it cannot be changed.

Example:

var arr [5]int = [5]int{1, 2, 3, 4, 5}  // Array of 5 integers
  • Accessing an element:
fmt.Println(arr[0])  // Outputs: 1

Slices

A slice is a dynamically-sized, more flexible version of an array. Slices are widely used in Go because they can grow and shrink as needed.

Example:

var numbers []int = []int{1, 2, 3, 4, 5}  // A slice of integers
numbers = append(numbers, 6) // Adding an element to the slice

Maps

A map is an unordered collection of key-value pairs, where each key is unique. Maps are used when you need to store values associated with keys and quickly retrieve them.

Example:

var ages map[string]int = make(map[string]int)  // Declare and initialize a map
ages["Alice"] = 30
ages["Bob"] = 25
fmt.Println(ages["Alice"])  // Outputs: 30

Structs

A struct is a composite type that groups together variables of different data types under a single name. It’s used to create custom data types.

Example:

type Person struct {
Name string
Age int
}
p := Person{Name: "John", Age: 30}  // Creating an instance of the struct
fmt.Println(p.Name) // Accessing fields: Outputs "John"

3. Pointer Types

Pointers store the memory address of another variable. They are useful when you want to reference a value indirectly or pass large structures efficiently.

  • Declaring a pointer: Use the * symbol to declare a pointer type and the & symbol to get the address of a variable.

Example:

var x int = 42
var p *int = &x // Pointer to an integer
fmt.Println(*p)   // Dereferencing the pointer to get the value: Outputs 42

4. Function Types

In Go, functions are first-class citizens, meaning they can be assigned to variables, passed as arguments, and returned from other functions.

Example:

func add(a int, b int) int {
return a + b
}
var sumFunc func(int, int) int = add
fmt.Println(sumFunc(3, 4)) // Outputs: 7

5. Interface Types

An interface in Go is a type that specifies a set of methods that a type must implement. It’s used to define behavior and allow for polymorphism.

Example:

type Shape interface {
Area() float64
}
type Circle struct {
Radius float64
}
func (c Circle) Area() float64 {
return 3.14 * c.Radius * c.Radius
}
var s Shape = Circle{Radius: 5}
fmt.Println(s.Area()) // Outputs: 78.5

6. Type Conversion

In Go, you can convert values between compatible types using type conversion. Unlike some languages, Go does not support implicit type conversion, so you need to do it explicitly.

Example:

var a int = 42
var b float64 = float64(a) // Explicit conversion from int to float64
fmt.Println(b) // Outputs: 42.0

7. Constants

A constant is a value that cannot be changed once it’s declared. Constants can be of any basic type: string, bool, or a numeric type.

Example:

const Pi = 3.14
const IsReady bool = true

Summary of Common Data Types:

CategoryTypes

Booleanbool

Integerint, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64

Floating-pointfloat32, float64

Complexcomplex64, complex128

Stringstring

Derived Typesarrays, slices, maps, structs

Pointer* (pointer to another type)

Functions Functions as first-class types

Interface Interface types that describe a set of methods

Go is a compiled language, and it produces optimized binaries for direct execution on the target machine.

Why Go was Designed:

  1. Address complexity and inefficiencies in existing languages like C++ and Java (focusing on simplicity and developer productivity).
  2. Improve concurrency support with lightweight goroutines and channels to handle modern multi-core processors.
  3. Faster compilation and iteration times for large-scale projects.
  4. Provide robust and efficient tooling, a powerful standard library, and cross-platform support.
  5. Make concurrent programming easier for developers by offering a simpler concurrency model

--

--

No responses yet