Trusted answers to developer questions

What are the basic data types in Go?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

We can broadly classify the basic data types of the Go language into the following categories.

Basic data types

Type

Description

Boolean

Two predefined values, True and False.

Numeric

Arithmetic type which includes integers and floating point.

String

Simply represents the string values. (Immutable)

Go offers both implicit and explicit types. In the case of implicit type, the untyped constant is implicitly converted to a typed constant where required. For example: i := 0, same as var i int = 0.

Boolean

It can only store one of the two values:

  • True
  • False

Code

The following code shows how Boolean can be used in Go.

package main
import "fmt"
func main() {
string1 := "educative"
string2 := "educative"
string3 := "edpresso"
result1 := string1 == string2
result2 := string1 == string3
// Shows the result and type
fmt.Println("Value of result1 :", result1)
fmt.Printf("Type of result1 : %T\n", result1)
fmt.Println("Value of result2 :", result2)
fmt.Printf("Type of result2 : %T\n", result2)
}

Numeric

Numeric types are used to represent different types of numbers. The following table shows the sub-categories of numeric types.


Type

Description

Integers




uint8

Unsigned 8-bit integers

0 to 255


uint16

Unsigned 16-bit integers

0 to 65535


uint32

Unsigned 32-bit integers

0 to 4294967295


uint64

Unsigned 64-bit integers

0 to 4294967295


int8

Signed 8-bit integers

-128 to 127


int16

Signed 16-bit integers

-32768 to 32767


int32

Signed 32-bit integers

-2147483648 to 2147483647


int64

Signed 64-bit integers

-9223372036854775808 to 9223372036854775807

Floating Point


Approximate


float32

(IEEE-754) floating-point 32-bit numbers


float64

(IEEE-754) floating-point 64-bit numbers


complex64

Complex numbers with float32 as a real and imaginary part.


complex128

Complex numbers with float64 as a real and imaginary part.

  • Float value cannot be assigned to int data type. For example: var i int8 = 0.9877 will return an error.
  • Int value can be stored in float data type. For example: var i float = 1 will not return an error.

Code

The following code shows how different numeric data types can be used in Go.

package main
import "fmt"
func main() {
// 8-bit unsigned int
var A uint8 = 225
fmt.Println("Value of A :", A)
fmt.Printf("Type of A : %T\n", A)
// 8-bit signed int
var B int8 = -112
fmt.Println("Value of B :", B)
fmt.Printf("Type of B : %T\n", B)
// float32
var C float32 = 0.6046603
fmt.Println("Value of C :", C)
fmt.Printf("Type of C : %T\n", C)
// complex64
var D complex64 = complex(10, 20)
fmt.Println("Value of D :", D)
fmt.Printf("Type of D : %T\n", D)
}

String

This represents the string values. Strings are immutable, which means that once they are created, it is impossible to change their contents.

Code

The following code shows how strings can be used in Go.

package main
import "fmt"
func main() {
string1 := "Educative"
fmt.Println("Value of string is:", string1)
fmt.Printf("Type of string is: %T", string1)
}

Rune is a superset of ASCII and represents Unicode codepoints. It is an alias for int32. For example, the rune literal (‘A’ )is actually the number 65. Below is an example of rune.

package main
import "fmt"
func main() {
var runeVariable rune = 'A'
fmt.Println("Value of runeVariable is:", runeVariable)
fmt.Printf("Type of runeVariable is: %T", runeVariable)
}

RELATED TAGS

go
data types
Did you find this helpful?