Go Data Types
Every value in a program has a data type, and this idea matters a lot: the type tells Go what kind of value a variable holds and how much space it takes up.
Types in Go are static, so a variable keeps the type you give it and will never accept data of another type later.
The language has three basic groups of types:
- bool - a value that is either
trueorfalse - Numeric - whole numbers (integers), floating-point numbers, and complex numbers
- string - a run of characters, such as a piece of text
Example
Here is one variable from each group, with the numeric group shown as both an integer and a float:
Prompt
Declare variables of type bool, int, float32, and string. Print each with a label.
Output: Go program and captured output
main.go
package main
import ("fmt")
func main() {
var a bool = true
var b int = 5
var c float32 = 3.14
var d string = "Hi!"
fmt.Println("Boolean: ", a)
fmt.Println("Integer: ", b)
fmt.Println("Float: ", c)
fmt.Println("String: ", d)
}