Flag tag Usage in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

As in other languages, the advantage of using different parameters on a Linux system to implement different functions for code to perform different logic is to execute the desired logic without modifying the code to recompile and package. In the Golang language also provides us with the corresponding function, directly on the code, in the code will do a detailed explanation, the current operating environment is: Go-1.8.1

Package Mainimport ("Flag" "Log" "OS") Func usage () {log. Fatalf ("Usage:myprogram [-S server] [-t isshowtimestamps] <subject> \ n")}func printmsg (message string) {log. Printf ("Received message is:%s \ n", message)}func main () {//os. Args provides the raw command-line parameter access functionality. Note: The first parameter in the slice is the path to the program, and the OS.    Args[1:] Save all the parameters. Argsall: = os. Args log. Println ("Argsall:", Argsall)//Get the parameter content that is meaningful to us argsuseful: = OS. Args[1:] Log. Println ("argsuseful:", argsuseful)/** The first parameter: sets the corresponding label name, which can be obtained by the tag name or the second parameter: if the label is not set, then the value is the third parameter of the default value: This parameter For help information, it is generally used to show */var message = flag. String ("s", "Default message", "It's user send Message[help message]") var showTime = flag. Bool ("T", false, "Display timestamps")/** format log input, default: Log. Lstdflags (underlying equivalence: Ldate | Ltime), ldate:2017/04/01, ltime:16:24:36, Llongfile: Full path + Execute file + number of rows, Lshortfile: Execute file name + number of lines, there are several other infrequently used, if necessary, can be on the official documents to view the settings here 0 that is, cancel the log format output, the output content and use the FMT package under the println () format *///log. SetFlags (0) log. SetFlags (log. LSTDFLAGS)//Initialize the anonymous usage function built into the flag package, you need to assign a function. When an exception occurs inside flag, it calls its internal usage function and then calls to our own defined usage function flag. Usage = usage//After all flags have been declared complete, call Flag.parse () to execute command line parsing flag. Parse ()//The user does not have any parameter input to execute the args: = flag. Args () If Len (args) < 1 {usage ()} log. PRINTLN ("message:", *message, ", ShowTime:", *showtime) printmsg (*message)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.