golang flag簡單用法

大囚長發表於2018-07-09
package main

import (
    "flag"
    "strings"
    "os"
    "fmt"
)

var ARGS string

func main() {

    var uptime *bool = new(bool)
    flag.BoolVar(uptime,"u", false, "print system uptime")
    flag.Parse()

    ARGS = strings.Join(flag.Args(), " ")
    if len(os.Args) < 2 {
        flag.Usage()
        os.Exit(1)
    }

    if *uptime {
        fmt.Println("12 days")
    }
}

相關文章