Datatypes in Go

In this tutorial, we will learn about the basic datatypes provided in Go language.

Categorically, there are three different types of basic datatypes based on the kind of values they can allow. They are

  • Numeric datatypes – these datatypes allow integer and floating point values.
  • Boolean or Logical – this datatype can allow logical values.
  • Strings – this datatype can allow a sequence of characters.

Numeric Datatypes

There are different kinds of numeric datatypes, especially types that can store integer values, based on the storage and capability to store signed/unsigned values in it. Also we have two types of datatypes float32 and float64 that can store floating point values.

The following table lists out the different datatypes with the keyword and its description.

Data TypeDescription
int88-bit signed integer
int1616-bit signed integer
int3232-bit signed integer
int6464-bit signed integer
uint88-bit unsigned integer
uint1616-bit unsigned integer
uint3232-bit unsigned integer
uint6464-bit unsigned integer
intBoth int and uint contain same size, either 32 or 64 bit.
uintBoth int and uint contain same size, either 32 or 64 bit.
runeIt is a synonym of int32 and also represent Unicode code points.
byteIt is a synonym of int8.
uintptrIt is an unsigned integer type. Its width is not defined, but its can hold all the bits of a pointer value.
float3232-bit IEEE 754 floating-point number
float6464-bit IEEE 754 floating-point number

In the following example, we define a variables of different types of numeric datatypes.

var x int = 65
var y float32 = 3.14
var z byte = 87
var p rune = 362
ADVERTISEMENT

Boolean Datatype

Value of type boolean can store only one bit of data. Its either 1 or 0. 1 represents true and 0 represents false. bool keyword is used to define a variable or constant of type boolean.

true and false are the allowed values for a boolean value.

In the following example, we define a variable x of type boolean, and we assign boolean values to it.

var x bool
x = true
x = false

String Datatype

A string datatype specifies that the variable or constant can store a sequence of characters.

In the following example, we define a variable x of type string, and we assign a string to it.

var x string
x = "Hello World!"

Conclusion

In this Golang Tutorial, we learned different basic datatypes in Go language, and how to declare or define variables with these datatypes.