Go Data Types

www.igif‮.aedit‬com

Go is a statically typed programming language, which means that each variable and expression has a specific data type that is determined at compile time. Here are the basic data types available in Go:

Numeric types

Go supports several types of numeric data, including integers, floating-point numbers, and complex numbers. Here are some examples:

  • int and uint: signed and unsigned integers with a size of either 32 or 64 bits, depending on the architecture
  • int8, int16, int32, and int64: signed integers with a size of 8, 16, 32, and 64 bits, respectively
  • uint8, uint16, uint32, and uint64: unsigned integers with a size of 8, 16, 32, and 64 bits, respectively
  • float32 and float64: floating-point numbers with a size of 32 and 64 bits, respectively
  • complex64 and complex128: complex numbers with a size of 64 and 128 bits, respectively

Boolean type

The bool type represents a Boolean value, which can be either true or false.

var b bool = true

String type

The string type represents a sequence of Unicode characters. In Go, strings are immutable, which means that once you create a string, you cannot modify its contents.

var s string = "Hello, world!"

Other types

Go also includes several other built-in types, including:

  • byte: alias for uint8
  • rune: alias for int32, used to represent Unicode code points
  • uintptr: an unsigned integer large enough to hold the bit pattern of any pointer value
  • error: an interface type used to represent an error condition

In addition to the built-in types, Go also supports user-defined types, such as structs, arrays, and slices. With structs, you can define custom data types by combining multiple fields of different types. Arrays and slices, on the other hand, are used to represent a collection of values of the same type.

// Define a struct type
type Person struct {
    Name string
    Age  int
}

// Create an array of integers
var a [3]int = [3]int{1, 2, 3}

// Create a slice of strings
var s []string = []string{"foo", "bar", "baz"}

Understanding the basic data types available in Go is important for writing correct and efficient code.