Choose bool and string in Go
Two basic types for everyday data
A bool holds only true or false. A string holds an immutable sequence of bytes, often UTF-8 text. Use a boolean for a state such as whether an order is paid, and a string for a name or message. Quotes turn true into text, not a boolean.
package main
import "fmt"
func main() { var paid bool = true var customer string = "Ada" fmt.Println(customer, paid)}The output is Ada true. The two variables have different types. The zero value of bool is false; the zero value of string is the empty string. You can assign a new string to a variable but cannot modify an individual byte of its current string.
Text literal forms
package main
import "fmt"
func main() { fmt.Println("A\nB") fmt.Println(`A\nB`)}Double quotes interpret escape sequences such as \n as a line break. Backticks preserve the backslash and the letter n here. The first call prints A and B on separate lines; the second prints the literal text A\nB. Do not assign "false" to a bool variable.
Try it locally
Declare a string product with value Book and a bool inStock with value false. Print both using Println. Expected: Book false. Hint: use quotes only for the product text.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Constants and Basic Types 01: Choose bool and string in Go
Check the lesson objective before moving on.
Which value has type bool?
What does a raw string `A\nB` contain?