Booleans in Go

A Boolean Value is a value with only two possible states: true or false. In Go if no value is provided to boolean variable then false is set as the default.

Go Boolean Example

Here's a boolean example in Go:

// create main package
//The package “main” tells the Go compiler that the package should compile as an executable
// program instead of a shared library.
package main

//fmt -> A Package that implements formatted I/O with functions analogous to C's printf and scanf.
import "fmt"

//create main function ->  the entry point of our executable program.
func main() {
    // create a boolean variable and assign it a value
    var me bool = true
    //Printf -> formats strings according to a format specifier and writes to standard output.
    //%v -> the value in a default format when printing structs
    // \n -> new line operator
    fmt.Printf("me is: '%v'\n", me)

    //you can reassign thevalue of the me variabe
    me = false
    fmt.Printf("me is: '%v'\n", me)

    var b2 bool
    fmt.Printf("zero value of bool is: '%v'\n", b2)
    // :show end
}

If you run it you get the following:

me is: 'true'
me is: 'false'
zero value of bool is: 'false'

We will be happy to hear your thoughts

Leave a reply

94 − ninety =

Camposha
Shopping cart