Bool flag argument

Report a typo

Below you will see a Go program greet_with_color.go that greets the user with or without a random colored output by passing arguments to the --name and --colored flags:

package main

import (
    "flag"
    "fmt"
    "math/rand"
    "time"
)

func main() {
    name := flag.String("name", "User", "Enter your name")

    var colored bool
    flag.BoolVar(&colored, "colored", true, "Use a random color for the output")

    flag.Parse()
    greetUser(name, colored)
}

func greetUser(name *string, colored bool) {
    if colored {
        seed := time.Now().UnixNano()
        rand.Seed(seed)
        randomRGB := rand.Intn(256*256*256 - 1)
        fmt.Printf("\033[38;2;%d;%d;%dm", randomRGB/256/256, randomRGB/256%256, randomRGB%256)
    }
    fmt.Printf("Hello, %s! Have a nice day!\033[0m", *name)
}

Select all the correct ways to call the greet_with_color program to print a non-colored output.

Tip: Remember which special character is mandatory when specifying a value for a bool flag.

Select one or more options from the list
___

Create a free account to access the full topic