Home > Backend Development > Golang > Introduction to Go command line parameters and standard input and output

Introduction to Go command line parameters and standard input and output

Release: 2020-02-20 17:33:22
forward
4219 people have browsed it

Go language is a statically strongly typed, compiled, concurrent programming language with garbage collection function developed by Google. The following column will introduce you to the Go command line parameters and standard input and output from the go Getting Started Tutorial column.

Introduction to Go command line parameters and standard input and output

1. Use of Go command line parameters

Go’s command line parameters are stored in the slice os.Args, which can be said to be the same as python’s command line parameters. Very similar

fmt.Println(os.Args)//打印切片内容
for i := 0; i < len(os.Args); i++ {
    fmt.Println(os.Args[i])
}
Copy after login

The first parameter is the name of the executable file, the other parameters are in the form of strings, stored in slice os.Args, you can use the for range statement to traverse all parameters

for i, args := range os.Args {
    fmt.Printf("args[%d]=%s\n",i,args)
}
Copy after login

2. Parsing of command line parameters by flag package

The above parameter parsing only stores the parameters in the os.Args slice from the command line. It is not very convenient when applied. In particular, when others don't know how to use a compiled executable file, they can use Go's built-in flag package to explain the parameters and set default values.

How to use the flag package

flag.Type("flagName",defaultValue,"help message") *Type
Copy after login

The flag package sets the default value and help information for the flag flagName based on the type of Type and the parameter flag flagName, and finally returns a pointer to the type. You can Determine whether the flag parameter is used in the command line by whether the pointer is empty. The following is an example.

import(
    "flag"
    "fmt"
)
var n = flag.Int("n",1,"number of page")
var s = flag.String("s","nothing","info")
func main() {
    flag.Parse()
    fmt.Println(*n)
    fmt.Println(*s)
}
Copy after login

Execute ./cmd –help and you can see the set parameter help information

Generally, parameter settings are used in the init function before the main function starts, so that you can directly use it in the main function Use

3, standard input and output

Standard input and output are very commonly used in daily programs, generally reading, printing, etc.

Read data from standard input

func main() {
    input := bufio.NewScanner(os.Stdin)//初始化一个扫表对象
    for input.Scan() {//扫描输入内容
        line := input.Text()//把输入内容转换为字符串
        fmt.Println(line)//输出到标准输出
    }
}
Copy after login

For more go language knowledge, please pay attention to the go language tutorial column on the php Chinese website.

The above is the detailed content of Introduction to Go command line parameters and standard input and output. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
go
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template