i Go – data types – All things in moderation

Go – data types

The data type in Go is static, which cannot be changed unlike some languages such as PHP, Javascript … that allow changing the data type during the run.

The types in Go can be classified as follows

Type Description
Boolean types

They are boolean types and consists of the two predefined constants: (a) true (b) false

Numeric types

They are again arithmetic types and they represents a) integer types or b) floating point values throughout the program.

String types

A string type represents the set of string values. Its value is a sequence of bytes. Strings are immutable types that is once created, it is not possible to change the contents of a string. The predeclared string type is string.

Derived types

They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types f) Slice types g) Interface types h) Map types i) Channel Types

In this section we will learn about some of the basic data types in Go: integer type, Floating Types, string type and boolean type.

Integer Types

The predefined architecture-independent integer types are

Type Description
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 18446744073709551615)
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 Types

The predefined architecture-independent float types are −

Type Description
float32 IEEE-754 32-bit floating-point numbers
float64 IEEE-754 64-bit floating-point numbers
complex64 Complex numbers with float32 real and imaginary parts
complex128 Complex numbers with float64 real and imaginary parts

Other Numeric Types

There is also a set of numeric types with implementation-specific sizes −

Type Description
byte same as uint8
rune same as int32
uint 32 or 64 bits
int same size as uint
uintptr an unsigned integer to store the uninterpreted bits of a pointer value

String Type

Strings are characters enclosed in double quotes (“) or single quotes (‘) used to represent text.

We can perform some common operations on String such as calculating string length, taking characters at certain positions, linking strings.

For example:

string.go

package main

import “fmt”

func main() {
fmt.Println(len(“String in golang”))
fmt.Println(“String in golang”[1])
fmt.Println(“String” + “in golang”)
}

 

Output

11
101
Hello World
 

Boolean Types

Boolean values are 1-bit values, with two values, true and false, that used to represent the TRUE or FALSE meaning. In Go there are 3 operations that can be manipulated with boolean values are && (AND), || (OR ) and ! (NOT).

The following table describes how to perform operations with boolean types:

AND operater :

Operations Value
true && true true
true && false false
false && true false
false && false false

OR operater:

Operations Value
true || true true
true || false true
false || true true
false || false false

NOT operator:

Operations Value
!true false
!false true

Example:

boolean.go

package main

import “fmt”

func main() {

    fmt.Println(true && false)

    fmt.Println(true && false)

    fmt.Println(true || true)

    fmt.Println(true || false)

    fmt.Println(!true)

}

 

Output:

true
false
true
true
false

 

Leave a Reply