Go Data Types

Introduction

Data types are fundamental in programming as they define the type of data that can be stored and manipulated within a program. Go has a rich set of built-in data types that help developers write efficient and effective code. This guide will cover the basic data types in Go with examples for each.

Basic Data Types in Go

Integer Types

int

  • Description: A signed integer type that is platform-dependent (32-bit or 64-bit).
  • Example:
    var a int = 10
    fmt.Println(a) // Output: 10
    

int8

  • Description: A signed 8-bit integer (-128 to 127).
  • Example:
    var b int8 = -128
    fmt.Println(b) // Output: -128
    

int16

  • Description: A signed 16-bit integer (-32,768 to 32,767).
  • Example:
    var c int16 = 32767
    fmt.Println(c) // Output: 32767
    

int32

  • Description: A signed 32-bit integer (-2,147,483,648 to 2,147,483,647).
  • Example:
    var d int32 = 2147483647
    fmt.Println(d) // Output: 2147483647
    

int64

  • Description: A signed 64-bit integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).
  • Example:
    var e int64 = 9223372036854775807
    fmt.Println(e) // Output: 9223372036854775807
    

uint

  • Description: An unsigned integer type that is platform-dependent (32-bit or 64-bit).
  • Example:
    var f uint = 10
    fmt.Println(f) // Output: 10
    

uint8

  • Description: An unsigned 8-bit integer (0 to 255).
  • Example:
    var g uint8 = 255
    fmt.Println(g) // Output: 255
    

uint16

  • Description: An unsigned 16-bit integer (0 to 65,535).
  • Example:
    var h uint16 = 65535
    fmt.Println(h) // Output: 65535
    

uint32

  • Description: An unsigned 32-bit integer (0 to 4,294,967,295).
  • Example:
    var i uint32 = 4294967295
    fmt.Println(i) // Output: 4294967295
    

uint64

  • Description: An unsigned 64-bit integer (0 to 18,446,744,073,709,551,615).
  • Example:
    var j uint64 = 18446744073709551615
    fmt.Println(j) // Output: 18446744073709551615
    

Floating-Point Types

float32

  • Description: A 32-bit IEEE 754 floating-point number.
  • Example:
    var k float32 = 3.14
    fmt.Println(k) // Output: 3.14
    

float64

  • Description: A 64-bit IEEE 754 floating-point number.
  • Example:
    var l float64 = 2.71828
    fmt.Println(l) // Output: 2.71828
    

Complex Types

complex64

  • Description: A complex number with float32 real and imaginary parts.
  • Example:
    var m complex64 = 1 + 2i
    fmt.Println(m) // Output: (1+2i)
    

complex128

  • Description: A complex number with float64 real and imaginary parts.
  • Example:
    var n complex128 = 3 + 4i
    fmt.Println(n) // Output: (3+4i)
    

Boolean Type

bool

  • Description: Represents a boolean value, which can be either true or false.
  • Example:
    var o bool = true
    fmt.Println(o) // Output: true
    

String Type

string

  • Description: A sequence of characters.
  • Example:
    var p string = "Hello, Go!"
    fmt.Println(p) // Output: Hello, Go!
    

Rune Type

rune

  • Description: An alias for int32, represents a Unicode code point.
  • Example:
    var q rune = 'G'
    fmt.Println(q) // Output: 71
    

Byte Type

byte

  • Description: An alias for uint8, typically used to represent a byte of data.
  • Example:
    var r byte = 255
    fmt.Println(r) // Output: 255
    

Zero Values

In Go, variables are automatically assigned a zero value if not explicitly initialized. These zero values are:

  • int, int8, int16, int32, int64: 0
  • uint, uint8, uint16, uint32, uint64: 0
  • float32, float64: 0.0
  • complex64, complex128: (0+0i)
  • bool: false
  • string: ""
  • rune: 0
  • byte: 0

Example:

var s int       // s is 0
var t float64   // t is 0.0
var u bool      // u is false
var v string    // v is ""

Type Inference

Go can infer the type of a variable based on the value assigned to it using the := syntax.

Example:

w := 42        // int
x := 3.14      // float64
y := "Hello"   // string
z := true      // bool

Conclusion

Understanding Go’s basic data types is crucial for writing efficient and effective programs. These types include integers, floating-point numbers, booleans, strings, runes, and bytes. By knowing how to declare and use these data types, you can better manage the data in your Go programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top