Go Data Types
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:
intanduint: signed and unsigned integers with a size of either 32 or 64 bits, depending on the architectureint8,int16,int32, andint64: signed integers with a size of 8, 16, 32, and 64 bits, respectivelyuint8,uint16,uint32, anduint64: unsigned integers with a size of 8, 16, 32, and 64 bits, respectivelyfloat32andfloat64: floating-point numbers with a size of 32 and 64 bits, respectivelycomplex64andcomplex128: 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 foruint8rune: alias forint32, used to represent Unicode code pointsuintptr: an unsigned integer large enough to hold the bit pattern of any pointer valueerror: 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.
