zoukankan      html  css  js  c++  java
  • 1.3命令行参数获取 flag

    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 string
    	flag.StringVar(&logPrefix, "prefix", "", "Logger prefix")
    
    	var arr ArrayValue
    	flag.Var(&arr, "array", "Input array to iterate through.")
    
    	// Execute the flag.Parse function, to
    	// read the flags to defined variables.
    	// Without this call the flag
    	// variables remain empty.
    	flag.Parse()
    
    	// Sample logic not related to flags
    	logger := log.New(os.Stdout, logPrefix, log.Ldate)
    
    	retryCount := 0
    	for retryCount < *retry {
    		logger.Println("Retrying connection")
    		logger.Printf("Sending array %v
    ", arr)
    		retryCount++
    	}
    }
    
    /*
    
    ./go_web -h
    Usage of ./go_web:
      -array value
            Input array to iterate through.
      -prefix string
            Logger prefix
      -retry int
            Defines max retry count (default -1)
    
    */
    
    /*
    ./go_web  -retry  1   -prefix=test  -array=dssd,11,3453
    test2018/03/17 Retrying connection
    test2018/03/17 Sending array [dssd 11 3453]
    */
    
    
  • 相关阅读:
    前端——DOM
    前端——JavaScript
    前端——HTML
    初学Python——协程
    初学Python——进程
    初学Python——线程
    初学Python——Socket网络编程
    初学Python——RabbitMQ的安装
    初学Python——面向对象(二)
    muduo网络库源码学习————线程池实现
  • 原文地址:https://www.cnblogs.com/zrdpy/p/8592990.html
Copyright © 2011-2022 走看看