Go has three primary elementary types: Boolean, Numeric, and Character. These are fundamental building blocks used to represent different kinds of data in Go programs. Let's dive into each of them in detail.
Boolean Type
A Boolean represents one of two values: true
or false
. These are predefined constants in Go. Here's an example of declaring a Boolean variable:
var isAvailable bool = true
Booleans are commonly used in conditions and logical operations.
Numerical Type
Numerical types in Go are divided into two main categories: Integers and Floating-Point Numbers.
Integers
Go offers both architecture-dependent and architecture-independent integer types:
Architecture-Dependent Types:
int
,uint
, anduintptr
are architecture-dependent and their size (either 32-bit or 64-bit) is determined by the system on which the program runs.int
is a signed integer and is the default type for integers, whileuint
is unsigned.
Architecture-Independent Types:
These types have fixed sizes. For example:
int8
: Range from -128 to 127int16
: Range from -32,768 to 32,767int32
: Range from −2,147,483,648 to 2,147,483,647int64
: Range from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807Similarly,
uint8
,uint16
,uint32
, anduint64
are unsigned integers with different ranges.
Floating-Point Numbers
There are two floating-point types in Go:
float32
: Reliable up to about 7 decimal places.float64
: Reliable up to about 15 decimal places.
It's recommended to use float64
as it offers higher precision and all Go's math functions expect this type.
Floating-Point Notation Examples:
Octal Notation: Prefix
0
(e.g.,077
for the number 63).Hexadecimal Notation: Prefix
0x
(e.g.,0xFF
for the number 255).Scientific Notation: Use
e
for powers of 10 (e.g.,1e3
for 1000).
Important Note: Go is strongly typed, which means variables of different types cannot be mixed directly. However, constants can be mixed because they are treated as untyped.
Format Specifiers
When formatting output in Go, you use format specifiers to define how the data should be displayed:
%d
: For integers.%x
or%X
: For hexadecimal representation.%g
: For general float formatting (compact form).%f
: For floating-point values.%e
: For scientific notation.%c
: For characters.%U
: For Unicode code points.
Examples:
fmt.Printf("%d\n", 255) // Outputs: 255
fmt.Printf("%x\n", 255) // Outputs: ff
fmt.Printf("%g\n", 1.23456) // Outputs: 1.23456
Complex Numbers
In Go, complex numbers have two types:
complex64
: Consists of two 32-bit floating-point numbers (real and imaginary parts).complex128
: Consists of two 64-bit floating-point numbers.
A complex number is written as:
c1 := complex(5, 10) // 5 is the real part, 10 is the imaginary part
fmt.Println(c1) // Outputs: (5+10i)
To extract the real or imaginary parts, you can use the built-in real()
and imag()
functions.
Random Numbers
Go's math/rand
package generates pseudo-random numbers. To generate random numbers, you can use functions like rand.Intn(n)
to get a number in the range [0, n)
:
import "math/rand"
a := rand.Int() // Random integer
b := rand.Intn(7) // Random integer in range [0, 6]
The numbers generated are not truly random but are based on a seed.
Character Type
Strictly speaking, Go doesn't have a separate character type. Instead, characters are represented as integers (byte
or rune
).
byte
: Alias foruint8
, used for traditional ASCII characters.rune
: Alias forint32
, used for Unicode characters.
Example:
var ch byte = 'A' // The character 'A' in ASCII
var runeChar rune = '国' // A Unicode character
A byte is a single character in the ASCII table (e.g., 'A'
is 65 in decimal and 0x41 in hexadecimal). For Unicode characters, use rune
. The type rune
supports characters beyond the standard ASCII range.
To print different representations of a character:
fmt.Printf("%d\n", ch) // Decimal value
fmt.Printf("%X\n", ch) // Hexadecimal value
fmt.Printf("%c\n", ch) // Character itself
The unicode
Package
Go also provides the unicode
package for working with characters. Some useful functions include:
unicode.IsLetter(ch)
: Checks if the character is a letter.unicode.IsDigit(ch)
: Checks if the character is a digit.unicode.IsSpace(ch)
: Checks if the character is a whitespace.
These functions return a Boolean value and are helpful when processing text.
Now that you have a good understanding of the different data types, format specifiers, and how to handle characters, you're ready to explore the various operations you can perform on data in Go.