14:56:00, 12 April 2020

1, the flag

Complete sample
func main(a) {
    // Define command line argument mode 1
    var name string
    var age int
    var married bool
    var delay time.Duration
    flag.StringVar(&name, "name"."Zhang"."Name")
    flag.IntVar(&age, "age".18."Age")
    flag.BoolVar(&married, "married".false.The "marital status")
    flag.DurationVar(&delay, "d".0."Time interval of delay")

    // Parse command line arguments
    flag.Parse()
    fmt.Println(name, age, married, delay)
    // Returns the other arguments after the command line arguments
    fmt.Println(flag.Args())
    // Returns the number of other arguments after the command line argument
    fmt.Println(flag.NArg())
    // Returns the number of command line arguments used
    fmt.Println(flag.NFlag())
}
Copy the code
use

Prompt for command line arguments:

$ ./flag_demo -help Usage of ./flag_demo: -age int age (default 18) -d duration interval - Married Married no -name String name (default "zhang SAN ")Copy the code

2, select

 select {
    case <-chan1:
    // If chan1 successfully reads data, proceed with the case processing statement
    case chan2 <- 1:
    // If data is successfully written to CHAN2, the case processing statement is executed
    default:
    // If none of the above is successful, the default processing flow is entered
}
Copy the code

In a SELECT statement, the Go language evaluates each sent and received statement from beginning to end in order. If any of the statements can continue execution (that is, not be blocked), then use any of the statements that can be executed. If none of the statements can be executed (i.e., all channels are blocked), there are two possible scenarios:

  • If a default statement is given, the default statement is executed and the program’s execution resumes from the statement following the SELECT statement.
  • If there is no default statement, the SELECT statement will be blocked until at least one communication can proceed.
func main(a) {
    wg := &sync.WaitGroup{}
    wg.Add(1)
    go func(a) {
        defer wg.Done()
        / / processing
    }
    sigterm := make(chan os.Signal, 1)
    signal.Notify(sigterm, syscall.SIGINT, syscall.SIGTERM)
    
    // The following statement will block here, but as long as one case (default, if any) can be executed, proceed
    Wg.wait () means that all goroutinue added by Wg.add is finished
    select {
    case <-ctx.Done():
        log.Println("terminating: context cancelled")
    case <-sigterm:
        log.Println("terminating: via signal")
    }
    cancel()
    wg.Wait()
    iferr = client.Close(); err ! =nil {
        log.Panicf("Error closing client: %v", err)
    }
}
Copy the code

3, slicing

/ / reference / / http://www.45fan.com/article.php?aid=18012823094732488515550460 a [x, y, z] section length: y - x section capacity: z, xCopy the code