How to use Golang flag package (minimum knowledge)

The flag package is a convenient package for parsing command line flags.

import flag package

import "flag"

Define a variable to store the value of the flag

How to write when defining outside a function

When binding to a variable with XxxVar () of init () described later, prepare a variable to store the flag.

var boolFlag bool
var intFlag int
var strFlag string

How to write defined in a function

If it is not bound to a variable with XxxVar () of init () described later, it becomes a pointer.

  boolPtr := flag.Bool("b", false, "Example of boolean value")
  intPtr := flag.Int("i", 0, "Example for numerical values")
  strPtr := flag.String("s", "", "Example for strings")

Flag initialization

init () is executed before main () is called. The value of the flag specified in the variable by flag.Parse () described in main is entered, but the value is not yet entered in the variable at the time of init. Therefore, here, the behavior is only to reserve a pointer for storing the value of the flag. Bind to a variable with XxxVar (). Describe the pointer of the variable, the name of the flag, the default value, and the explanation of how to use it in the argument.

func init() {
  flag.BoolVar(&boolFlag, "b", false, "Specify a boolean value")
  flag.IntVar(&intFlag, "i", 0, "Specify a numeric value")
  flag.StringVar(&strFlag, "s", "", "Specify the value of the string")
}

Parsing and getting flags

Calling flag.Parse () parses the command line argument flags and binds them to variables.

flag.Parse()

After that, the value of the flag can be obtained from the variable.

Command line execution example

When executing from a source file

#No flag specified
go run flagsample.go

#With flag specified
go run flagsample.go -b
go run flagsample.go -b -i=1 -s=a

When executing from a binary file

#Compile the program and
go build flagsample.go
#Then run the binary.
./flagsample -b -i=1 -s=a

The big picture of the source

How to write when defining outside a function

package main

import (
  "flag"
  "fmt"
)

var boolFlag bool
var intFlag int
var strFlag string

func init() {
  flag.BoolVar(&boolFlag, "b", false, "Example of boolean value")
  flag.IntVar(&intFlag, "i", 0, "Example for numerical values")
  flag.StringVar(&strFlag, "s", "", "Example for strings")
}

func main() {
  flag.Parse()

  fmt.Println(boolFlag)
  fmt.Println(intFlag)
  fmt.Println(strFlag)
}

How to write defined in a function

package main

import (
  "flag"
  "fmt"
)

func main() {
  boolPtr := flag.Bool("b", false, "Example of boolean value")
  intPtr := flag.Int("i", 0, "Example for numerical values")
  strPtr := flag.String("s", "", "Example for strings")

  flag.Parse()

  fmt.Println(*boolPtr)
  fmt.Println(*intPtr)
  fmt.Println(*strPtr)
}

reference

-Go language learned with sample: Command-Line Flags

Recommended Posts

How to use Golang flag package (minimum knowledge)
How to use Nix package manager
Minimum knowledge to use Form in Flask
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use image-match
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use Pandas Rolling
[Note] How to use virtualenv
How to use redis-py Dictionaries
Python: How to use pydub
[Go] How to use "... (3 periods)"
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory
How to use Python bytes
How to use cron (personal memo)