英文:
What is the main difference between os.Args and os.Stdin?
问题
所以我一直在努力理解这两个函数的真正用途。
我只明白 Args 函数将用户输入作为 切片,而 Stdin 函数将其作为 字符串。那么这是否意味着我可以使用 Args 存储更多的值并更轻松地访问它们,而使用 Stdin 我只能一次存储最多一个输入?有人可以给出一个简单的解释和这两个函数的实际用途吗?谢谢
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
result := scanner.Text()
fmt.Println(result)
arguments := os.Args
fmt.Println(arguments[1:])
英文:
So I have been trying to understand what is the real use of these two functions.
I only understand the fact that Args gets user input as an slice and the Stdin as a string. So does that mean with Args I can store more values and access them more easily, and with stdin I only get to store at max one input at a time? Can somebody give a simple explanation and practical use of these two function? Thx
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
result := scanner.Text()
fmt.Println(result)
.
arguments := os.Args
fmt.Println(arguments[1:])
答案1
得分: 4
os.Stdin
是标准输入流,用于将数据从一个程序传递到另一个程序,或者用于动态用户输入,可用于创建基于文本的用户界面。
os.Args
是在调用程序时传递给程序的参数。例如 ./prog a b "hello world"
将得到 [ "prog", "a", "b", "hello world" ]
。这也是用于标志(flags)的机制。
你可以同时使用它们两个。
英文:
os.Stdin
is the standard input stream, used for piping data from one program to another or for dynamic user input which can be used to make text based user interfaces.
os.Args
are the arguments passed to the program when calling it. ./prog a b "hello world"
would result in ["prog", "a", "b", "hello world"]
. This is also the mechanism used for flags
You can use both at the same time.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论