Primitive Data Types - bool in Go
| Data Type | Size | Default Value |
|---|---|---|
| bool | Implementation-dependent | false |
Go is statically typed. A declaration can name a type explicitly or let the compiler infer it. Basic types include booleans, strings, integers, floating-point numbers, and complex numbers.
Tell a number from text
package main
import "fmt"
func main() {
fmt.Println(2 + 2)
fmt.Println("2" + "2")
}
4 22
A school shop sells three packs of paper at 125 cents each. Its scanner must print the code 007 exactly as it appears on the shelf. Keep the code as text and multiply the numeric quantity and price.
| Data Type | Size | Default Value |
|---|---|---|
| bool | Implementation-dependent | false |
A bool holds true or false. Go treats boolean and numeric values as distinct types.
The zero value of bool is false.
package main
import (
"fmt"
"strconv"
)
func main() {
// Declare and Initialize the local variable x of data type boolean
var x bool = true;
// Type cast the bool to string while on concatenation with a string
fmt.Println("The value of x is " + strconv.FormatBool(x));
// Declare a variable 'y' of type bool without initialization
var y bool
// Type cast the bool to string while on concatenation with a string
fmt.Println("The value of y without initialization is " + strconv.FormatBool(y));
}
The value of x is true The value of y without initialization is false
Use strconv.FormatBool to format a boolean as text.
| Data Type | Size | Default Value |
|---|---|---|
| string | Variable length | "" |
A Go string is an immutable sequence of bytes. A string literal may contain UTF-8 text.
The zero value of string is the empty string.
package main
import "fmt"
func main() {
var greeting string = "Hello"
var empty string
fmt.Println(greeting)
fmt.Println(len(empty))
}
Hello 0
Use len(s) to count bytes. Non-ASCII characters can occupy more than one byte.
| Type or declaration | Example | Use |
|---|---|---|
| string | "007" |
An immutable sequence of bytes, suitable for a written label. |
| int | 125 |
A signed integer whose size depends on the target architecture. |
| float64 | 0.25 |
A 64-bit floating-point number. |
| bool | false |
Its zero value is false. Conditions require a boolean value. |
| byte and rune | byte(65), 'é' |
Aliases for uint8 and int32. A rune may require multiple UTF-8 bytes. |
An uninitialized int starts at zero and an uninitialized bool starts at false. A Go string stores bytes. A rune represents a Unicode code point.
A name-tag printer reads the letter é in UTF-8. That one Unicode code point occupies two bytes. A byte limit and a character count can therefore give different answers for the same text.
Save this program as main.go and run it separately.
go run main.gopackage main
import (
"fmt"
"unicode/utf8"
)
func main() {
var pending int
var complete bool
text := "é"
var letter rune = 'é'
fmt.Println(pending, complete)
fmt.Println(len(text), utf8.RuneCountInString(text))
fmt.Printf("%d\n", letter)
}
0 false 2 1 233
The UTF-8 encoding of é occupies two bytes and contains one code point. A rune is an alias for int32, so %d prints its code point as 233.
Reference: Go language reference