Learning Go: Part 2 — Basic Data Types
Last time, we covered how to set up Go on a Linux machine and ran a Go file that printed out Hello, World!
to the terminal. That was a big step on our journey to learn Go, but there are still so many more aspects to the language that we need to cover.
Basic Data Types
A data type is a representation of data for programming languages. Go has several data types within it, and its basics are:
- bool
- string
- uint8, uint16, uint32, uint64, uintptr
- int8, int16, int32, int64
- byte
- rune
- float32, float64
- complex64, complex128
Examples
- bool
// Variable declaration
var TrueVariable bool = true
var FalseVariable bool = false
- string
var StringVariable string = "I am a string"// Shorthand variable declaration
OtherStringVariable := "I am a string"
- uint8, uint16, uint32, uint64, uintptr
The uint
data type represents an unsigned integer. The number at the end of the identifier (8, 16, etc.) represents how many bits there are. The normal uint
identifier is either 32 or 64 bits.
// Taken from "Data Types in Go" - GeeksForGeeks. (https://www.geeksforgeeks.org/data-types-in-go/)// Go program to illustrate
// the use of integers
package main
import "fmt"func main() { // Using 8-bit unsigned int
var X uint8 = 225
fmt.Println(X+1, X)
}
- int8, int16, int32, int64
The int
data type represents a signed integer. The number at the end of the identifier (8, 16, etc.) represents how many bits there are. The normal int
identifier is either 32 or 64 bits.
// Taken from "Data Types in Go" - GeeksForGeeks. (https://www.geeksforgeeks.org/data-types-in-go/)// Go program to illustrate
// the use of integers
package main
import "fmt"func main() { // Using 16-bit signed int
var Y int16 = 32767
fmt.Println(Y+2, Y-2)
}
- byte
A byte
is really an alias for a uint8
. This is used to distinguish characters from integer values. Because Go does not have a char
data type, it uses byte
and rune
to represent character values. The byte
data type represents ASCII characters.
var lastLetter byte = 'A'
- rune
A rune
is an alias for an int32
. As stated above, the rune
data type is used to distinguish characters from integer values. The rune
data type represents the broader set of Unicode characters that are encoded in UTF-8 format.
package mainimport "fmt"func main() {
// var x rune = 0x2318
var x rune = '⌘' fmt.Printf("%c\n", x)
}
- float32, float64
The float32
and the float64
data types are representative of the 32-bit and 64-bit IEEE 754 floating-point numbers respectively.
var a float32 = 30.45
var b float64 = 60.45
- complex64, complex128
In mathematics, a complex number is a combination of a Real Number and an Imaginary Number, and Go has a similar typing.
// Taken from "Data Types in Go" - GeeksForGeeks. (https://www.geeksforgeeks.org/data-types-in-go/)// Go program to illustrate
// the use of complex numbers
package main
import "fmt"func main() { var a complex128 = complex(6, 2)
var b complex64 = complex(9, 2)
fmt.Println(a)
fmt.Println(b) // Display the type
fmt.Printf("The type of a is %T and "+
"the type of b is %T\n", a, b)
}
Conclusion
In conclusion, Go supports many different types of data, and it’s up to the programmers to decide how these data types are used in their programs.
In the next posting, we will be going over functions
in the Go programming language and how we can use them and these data types to start building programs.
References
Data Types in Go. https://www.geeksforgeeks.org/data-types-in-go/
A Tour of Go
Golang Basic Types, Operators and Type Conversion. https://www.callicoder.com/golang-basic-types-operators-type-conversion/
Complex Numbers. https://www.mathsisfun.com/numbers/complex-numbers.html