Booleans
Create a true/false value.
1package main
2
3import "fmt"
4
5func main() {
6 isSunny := true
7 fmt.Println(isSunny)
8}
$ go run main.go
true
Every line, explained
package main- Every Go file starts by saying which package it belongs to. package main means "this is a program you can run", and Go will look inside it for func main.
import "fmt"- import brings in a package from Go's standard library. "fmt" (short for format) is the one full of printing tools, like fmt.Println.
func main() {- func main() { ... } is where a Go program starts. When you run the program, Go finds func main inside package main and runs everything between its braces, top to bottom.
isSunny := true- A boolean (Go calls the type bool) can only be true or false, both lowercase. Go names use camelCase, like isSunny. Keep your own variables starting lowercase: a capital first letter makes a name public in Go, visible to any package that imports yours.
fmt.Println(isSunny)- fmt.Println(...) prints what is inside the brackets to the terminal, then moves to a new line (Println is short for "print line"). The capital P matters: in Go, the tools a package shares all start with a capital letter. Printing a boolean shows the word true or false.
}- This closing brace } ends func main.