×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang Data Types

By IncludeHelp Last updated : October 05, 2024

Data Types in Go Language

Go data types are used to describe the type of the data. A data type tells the type of data that a Go variable can store. Memory for a variable is also created based on the data type.

Go language has many built-in data types which we will now read in more detail.

The Golang data types can be classified as follows –

  1. Basic Types
    1. Boolean Types
      1. true
      2. false
    2. Numeric Types
      1. Integer Types
      2. Floating Types
      3. Complex Types
    3. String Types
  2. Aggregate Types
    1. Arrays
    2. Structures
  3. Reference Types
    1. Pointers
    2. Slices
    3. Maps
    4. Functions
    5. Channels
  4. Interface Types

In this Golang Data Types tutorial, we will learn about the basic types only.

Golang Basic Data Types

The basic data types further divided into 3 categories,

  1. Boolean Types
  2. Numeric Types
  3. String Types

(a) Go Boolean Types

true and false are only two values that come under Boolean types, it represents only one bit of the information.

The bool keyword is used to declare a Boolean data type. And, its default value is false.

Example 1

// Golang program to demonstrate the
// example of Boolean types

package main

import (
	"fmt"
)

func main() {
	// declaring Boolean variables
	var x bool = true
	var y bool = false
	var z bool // default value is false

	// printing the values
	fmt.Println("x = ", x)
	fmt.Println("y = ", y)
	fmt.Println("z = ", z)
}

Output:

x =  true
y =  false
z =  false

Example 2

// Golang program to demonstrate the
// example of Boolean operations

package main

import (
	"fmt"
)

func main() {
	x := 10
	y := 20

	// printing the result and types
	fmt.Printf("Expression: (x == y), Result: %t, Result Type: %T\n", x == y, x == y)
	fmt.Printf("Expression: (x != y), Result: %t, Result Type: %T\n", x != y, x != y)
	fmt.Printf("Expression: (x < y),  Result: %t, Result Type: %T\n", x < y, x < y)
	fmt.Printf("Expression: (x > y),  Result: %t, Result Type: %T\n", x > y, x > y)
	fmt.Printf("Expression: (x <= y), Result: %t, Result Type: %T\n", x <= y, x <= y)
	fmt.Printf("Expression: (x >= y), Result: %t, Result Type: %T\n", x >= y, x >= y)
}

Output:

Expression: (x == y), Result: false, Result Type: bool
Expression: (x != y), Result: true, Result Type: bool
Expression: (x < y),  Result: true, Result Type: bool
Expression: (x > y),  Result: false, Result Type: bool
Expression: (x <= y), Result: true, Result Type: bool
Expression: (x >= y), Result: false, Result Type: bool

(b) Go Numeric Types

In Go language, numeric data types are,

  1. Integers
  2. Floating-Point Numbers
  3. Complex Numbers

(i) Integers

Both signed and unsigned integers types are available in the Go language. These types are available in 4 different sizes 8 bits, 16 bits, 32 bits, and 64 bits.

The integer types are –

Data Type Size Value Range
Signed
uint8 8 bits 0 to 255
uint16 16 bits 0 to 65535
uint32 32 bits 0 to 4294967295
uint64 64 bits 0 to 18446744073709551615
Unsigned
int8 8 bits -128 to 127
int16 16 bits -32768 to 32767
int32 32 bits -2147483648 to 2147483647
int64 64 bits -9223372036854775808 to 9223372036854775807
Others
byte 8 bits Synonyms of int8
int 32 bits or 64 bits based on the system architecture Signed integer value based on the size occupied.
uint 32 bits or 64 bits based on the system architecture Unsigned integer value based on the size occupied.
rune 32 bits Similar as int32, also represents Unicode code points.
uintptr Not defined Can store the uninterpreted bits of a pointer value.

Example:

// Golang program to demonstrate
// signed & unsigned integers

package main

import (
	"fmt"
)

func main() {
	// unsigned integers
	var a uint8 = 0
	var b uint16 = 0
	var c uint32 = 0
	var d uint64 = 0

	// signed integers
	var e int8 = -128
	var f int16 = -32768
	var g int32 = -2147483648
	var h int64 = -9223372036854775808

	fmt.Println("Minimum values....")
	fmt.Println("a = ", a)
	fmt.Println("b = ", b)
	fmt.Println("c = ", c)
	fmt.Println("e = ", e)
	fmt.Println("f = ", f)
	fmt.Println("g = ", g)
	fmt.Println("h = ", h)

	a = 255
	b = 65535
	c = 4294967295
	d = 1844674407370955161
	e = 127
	f = 32767
	g = 2147483647
	h = 9223372036854775807

	fmt.Println("Maximum values....")
	fmt.Println("a = ", a)
	fmt.Println("b = ", b)
	fmt.Println("c = ", c)
	fmt.Println("d = ", d)
	fmt.Println("e = ", e)
	fmt.Println("f = ", f)
	fmt.Println("g = ", g)
	fmt.Println("h = ", h)
}

Output:

Minimum values....
a =  0
b =  0
c =  0
e =  -128
f =  -32768
g =  -2147483648
h =  -9223372036854775808
Maximum values....
a =  255
b =  65535
c =  4294967295
d =  1844674407370955161
e =  127
f =  32767
g =  2147483647
h =  9223372036854775807

(ii) Floating-Point Numbers

Golang has two data types for floating-point numbers which are float32 and float64.

Data Type Size Value Range
float32 32 bits Single precision floating, IEEE 754 floating-point number
float64 64 bits Double precision, IEEE 754 floating-point number

Example:

// Golang program to demonstrate
// the floating-point types

package main

import (
	"fmt"
)

func main() {
	var a float32 = 12.123456789
	var b float64 = 12.123456789

	fmt.Println("a = ", a)
	fmt.Println("b = ", b)
}

Output:

a =  12.123457
b =  12.123456789

(iii) Complex Numbers

Complex numbers consist of two parts real and imaginary. Go language has two data types for complex numbers which are complex64 and complex128. To create a complex number variable, Golang has built-in functions.

Data Type Size Value Range
complex64 64 bits Complex numbers containing float32 value as real part and imaginary component.
complex128 128 bits Complex numbers containing float64 value as real part and imaginary component.

Example:

// Golang program to demonstrate
// the complex numbers

package main

import (
	"fmt"
)

func main() {
	var a complex64 = complex(123, 456)
	var b complex128 = complex(123, 456)

	fmt.Println("a = ", a)
	fmt.Println("b = ", b)
}

Output:

a =  (123+456i)
b =  (123+456i)

(c) Go String Types

In Go language, the string is a sequence of immutable bytes (Unicode characters) that cannot be changed.

Example:

// Golang program to demonstrate
// the string types

package main

import (
	"fmt"
)

func main() {
	var str1 string = "New Delhi, India"

	var str2 string
	str2 = "Hello, world!"

	str3 := "Hi, there?"

	fmt.Println("str1 = ", str1)
	fmt.Println("str2 = ", str2)
	fmt.Println("str3 = ", str3)
}

Output:

str1 =  New Delhi, India
str2 =  Hello, world!
str3 =  Hi, there?



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.