Data Types

Data Types

Go is a statically typed programming language. Once the variable is defined, its data type cannot change. There are Primitive and Complex data types. Each data type behaves differently, and understanding those differences is crucial for their efficient usage.

Primitive Data Types #

Primitive types
TypeZero (Default) ValueStorage Size (Bytes)Min-Max ValuesNote
int04 or 8int32 or int64 min-max valuesThis type is 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems
int801-128 to 127
int1602-32768 to 32767
int3204-2147483648 to 2147483647
int6408-9223372036854775808 to 9223372036854775807
uint04 or 8int32 or int64 min-max valuesThis type is 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems
uint8010 to 255
uint16020 to 65535
uint32040 to 4294967295
uint64080 to 18446744073709551615
float32041.401298464324817e-45 to 3.4028234663852886e+38IEEE-754 32-bit floating-point numbers
float64084.940656458412465e-324 to 1.7976931348623157e+308IEEE-754 64-bit floating-point numbers
complex64(0+0i)8complex numbers with float32 real and imaginary parts
complex128(0+0i)16complex numbers with float64 real and imaginary parts
boolfalse1
string''8 for UTF-8 and 16 for UTF-16
byte010 to 255alias for uint8
rune04-2147483648 to 2147483647alias for int32
uintptr0an unsigned integer large enough to store the uninterpreted bits of a pointer value

------ Execution ------


Type: int >> Zero Value: 0 >> Storage Size: 8 >> Min.Value: -9223372036854775808 >> Max.Value: 9223372036854775807
Type: int8 >> Zero Value: 0 >> Storage Size: 1 >> Min.Value: -128 >> Max.Value: 127
Type: int16 >> Zero Value: 0 >> Storage Size: 2 >> Min.Value: -32768 >> Max.Value: 32767
Type: int32 >> Zero Value: 0 >> Storage Size: 4 >> Min.Value: -2147483648 >> Max.Value: 2147483647
Type: int64 >> Zero Value: 0 >> Storage Size: 8 >> Min.Value: -9223372036854775808 >> Max.Value: 9223372036854775807
Type: uint >> Zero Value: 0 >> Storage Size: 8 >> Min.Value: 0 >> Max.Value: 18446744073709551615
Type: uint8 >> Zero Value: 0 >> Storage Size: 1 >> Min.Value: 0 >> Max.Value: 255
Type: uint16 >> Zero Value: 0 >> Storage Size: 2 >> Min.Value: 0 >> Max.Value: 65535
Type: uint32 >> Zero Value: 0 >> Storage Size: 4 >> Min.Value: 0 >> Max.Value: 4294967295
Type: uint64 >> Zero Value: 0 >> Storage Size: 0 >> Min.Value: 8 >> Max.Value: 18446744073709551615
Type: float32 >> Zero Value: 0 >> Storage Size: 4 >> Min.Value: 1.401298464324817e-45 >> Max.Value: 3.4028234663852886e+38
Type: float64 >> Zero Value: 0 >> Storage Size: 8 >> Min.Value: 5e-324 >> Max.Value: 1.7976931348623157e+308
Type: complex64 >> Zero Value: (0+0i) >> Storage Size: 8
Type: complex128 >> Zero Value: (0+0i) >> Storage Size: 16
Type: bool >> Zero Value: false >> Storage Size: 1
Type: string >> Zero Value:  >> Storage Size: 16
Type: byte >> Zero Value: 0 >> Storage Size: 1 >> Min.Value: 0 >> Max.Value: 255
Type: rune >> Zero Value: 0 >> Storage Size: 4 >> Min.Value: -2147483648 >> Max.Value: 2147483647

Numeric Data Types #

  • int, int8, int16, int32, int64 are used to define signed integer number types
  • uint, uint8, uint16, uint32, uint64 are used to define unsigned integer number types
  • Unsigned integer types only contain positive numbers and zero
  • int, uint, and uintptr are machine-dependent and usually 32 or 64 bits wide on 32bit or 64bit operating system
  • If there is no specific requirement, always use int or uint types for integers
  • uintptr is an integer representation of a memory address and GC does not consider them as live references to the memory blocks they point to. Only use it if you know what are you doing.
  • Default or zero value for numeric data types is 0

Floating Point Data Types #

  • float32 and float64 are used to define floating-point number types
  • float64 has better precision
  • If there is no specific requirement, always use float64 floating-point number types
  • Default or zero value for floating-point data types is 0

Complex Data Types #

  • complex64 and complex128 are used to define complex number types
  • complex64 contains 32 bit real and 32 bit imaginary part and complex128 has 64 bit real and 64 bit imaginary part
  • Default or zero value for complex data types is (0+0i)

Boolean Type #

  • bool is used to define a boolean type
  • bool type can only contain true or false values
  • Default or zero value for the boolean data type is false

String Type #

  • string is used to define a string type
  • string type is immutable and in fact it is a read-only slice of bytes
  • string type is defined between double quotes “my string” or backtick `my string`
  • byte and rune are used to represent character values.
  • byte is an alias for unit8 and rune is an alias for int32
  • rune represents a single character (unicode codepoint). Unicode character literal a-dieresis ‘รค’ holds two bytes (0xc3 0xa4) but representing one rune U+00E4
  • Default or zero value for the string data type is ’’ ‘’ (empty string)
  • Default or zero value for the byte or rune data types is 0

Composite Data Types #

Composite types
TypeDefault ValueStorage Size (Bytes)Note
errornil16
functionnil8
pointernil8
channelnil8
arrayEach element will have its type’s zero value
slicenil
mapnil
structEach field will have its type’s zero value
empty interfacenil

Examples #


------ Execution ------


Type: error >> Zero Value: <nil> >> Storage Size: 16
Type: function >> Zero Value: <nil> >> Storage Size: 8
Type: pointer >> Zero Value: <nil> >> Storage Size: 8
Type: channel >> Zero Value: <nil> >> Storage Size: 8
Type: array [1]int >> Zero Value: [0] >> Storage Size: 8
Type: array [1]string >> Zero Value: [] >> Storage Size: 16
Type: slice []int >> Zero Value: [] >> Storage Size: 24
Type: slice []string >> Zero Value: [] >> Storage Size: 24
Type: map [int]int >> Zero Value: map[] >> Storage Size: 8
Type: map [string]string >> Zero Value: map[] >> Storage Size: 8
Type: struct {int, string} >> Zero Value: {0 } >> Storage Size: 24
Type: interface {} >> Zero Value: <nil> >> Storage Size: 16

Error Type #

  • error type is an interface type
  • It is the conventional interface for representing an error condition, with the nil value representing no error.

Function Type #

  • A function type denotes the set of all functions with the same parameter and result types.
  • The value of an uninitialized variable of function type is nil.
  • Parameter and result lists are always parenthesized except that if there is exactly one unnamed result it may be written as an unparenthesized type. func a(x, y) (int) can be written as func a(x, y) int.
  • variadic function’s last parameter type defined with ... and fubction may be invoked with zero or more arguments for that parameter.
  • Two function types are identical if they have the same number of parameters and result values, corresponding parameter and result types are identical, and either both functions are variadic or neither is. Parameter and result names are not required to match.

Pointer Type #

  • A pointer type denotes the set of all pointers to variables of a given type, called the base type of the pointer.
  • The value of an uninitialized pointer is nil.
  • Two pointer types are identical if they have identical base types.

Channel Type #

  • A channel provides a mechanism for concurrently executing functions to communicate by sending and receiving values of a specified element type.
  • The value of an uninitialized channel is nil.
  • The optional <- operator specifies the channel direction, send or receive. If no direction is given, the channel is bidirectional.
  • The capacity, in number of elements, sets the size of the buffer in the channel. If the capacity is zero or absent, the channel is unbuffered and communication succeeds only when both a sender and receiver are ready.
  • Channel acts as FIFO (first in first out) queue. If one goroutine sends values on a channel and a second goroutine receives them, the values are received in the order sent.
  • Two channel types are identical if they have identical element types and the same direction.

Array Type #

  • An array is a numbered sequence of elements of a single type, called the element type.
  • The number of elements is called the length of the array and is never negative. The length is part of the array’s type.
  • The elements can be addressed by integer indices 0 through len(a)-1.
  • Array types are always one-dimensional but may be composed to form multi-dimensional types.
  • Two array types are identical if they have identical element types and the same array length.

Slice Type #

  • A slice is a descriptor of an underlying array segment. It provides access to a numbered sequence of elements from that array. A slice, once initialized, is always associated with an underlying array that holds its elements.
  • It consists of a pointer to the underlying array, the length of the segment, and its capacity (the maximum length of the segment).
  • The value of an uninitialized slice is nil.
  • The elements can be addressed by integer indices 0 through len(s)-1.
  • Slices are always one-dimensional but may be composed to construct higher-dimensional objects.
  • The length of the slice is the number of elements referred to by the slice. The capacity is the number of elements in the underlying array.
  • Two slice types are identical if they have identical element types.

Map Type #

  • A map is an unordered group of elements of one type, called the element type, indexed by a set of unique keys of another type, called the key type. The key type must not be a function, map, or slice.
  • The value of an uninitialized map is nil.
  • Elements may be added during execution using assignments and retrieved with index expressions; they may be removed with the delete built-in function.
  • Two map types are identical if they have identical key and element types.

Struct Type #

  • A struct is a sequence of named elements, called fields, each of which has a name and a type.
  • A field declared with a type but no explicit field name is called an embedded field.
  • Two struct types are identical if they have the same sequence of fields, and if corresponding fields have the same names, and identical types, and identical tags. Non-exported field names from different packages are always different.

Interface Type #

  • An interface type specifies a method set called its interface.
  • A variable of interface type can store a value of any type with a method set that is any superset of the interface.
  • The value of an uninitialized variable of interface type is nil.
  • An interface type may embed methods of other interfaces through interface type names. An interface type T may not embed itself or any interface type that embeds T, recursively.
  • The name of each explicitly specified method must be unique and not blank.
  • All types in Go implement the empty interface: interface{}. The value of an empty interface is nil.
  • Two interface types are identical if they have the same set of methods with the same names and identical function types. Non-exported method names from different packages are always different. The order of the methods is irrelevant.

Summary #

info Go is a statically typed programming language. Once the variable is defined its data type cannot change.
Go uses data types which are widly used in other languages, but it also has certain types which are specific to Go like rune, channel, complex.

Zero Value #

If variable declared but not initialized, then in Go they will get default value, also known as zero value, which is different per data type.

Type Inference #

If variable initialized without type declaration, then data type for the variable is inferred from the value on the right hand side.

a := 12 // i is integer type
b := a // b is integer type
msg := "Hello World" // msg is string type

Explicit Conversions #

Assignement of different type value to the variable requires explisit type conversion also known as type casting.

var a int = 12

// explicit type conversion
var b float64 = float64(a)