O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter01/recipe03.
  2. Navigate to the directory.
  3. Create the main.go file with the following content:
        package main        import (          "flag"          "fmt"          "log"          "os"          "strings"        )        // Custom type need to implement        // flag.Value interface to be able to        // use it in flag.Var function.        type ArrayValue []string        func (s *ArrayValue) String() string {          return fmt.Sprintf("%v", *s)        }        func (a *ArrayValue) Set(s string) error {          *a = strings.Split(s, ",")          return nil        }        func main() {           // Extracting flag values with methods returning pointers          retry := flag.Int("retry", -1, "Defines max retry count")          // Read the flag using the XXXVar function.          // In this case the variable must be defined          // prior to the flag. var logPrefix ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required