英文:
How to write the scanner in a separate function to make it work?
问题
在这段代码中,我创建了一个名为TakeInput()
的函数,它将接收用户输入,包括空格。但是,当我运行这段代码并输入姓名和学校名称时,它并没有打印出数据。
然而,如果我不使用任何函数,直接使用scanner
,它可以接收带有空格的数据。
package main
import (
"bufio"
"fmt"
"os"
)
func TakeInput(value string) {
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
value = scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Println("Error encountered:", err)
}
}
func main() {
var name, school string
fmt.Printf("Enter your name: ")
TakeInput(name)
fmt.Printf("Enter your school name: ")
TakeInput(school)
fmt.Println(name, school)
}
英文:
In this code, I have created a function TakeInput()
that will take the user input including the whitespaces also. But whenever I run this code and enter the name and school name, it does print the data for me.
Although if I write the scanner
without any function, it takes the data with whitespaces.
package main
import (
"bufio"
"fmt"
"os"
)
func TakeInput(value string) {
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
value = scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Println("Error encountered:", err)
}
}
func main() {
var name, school string
fmt.Printf("Enter your name: ")
TakeInput(name)
fmt.Printf("Enter your school name: ")
TakeInput(school)
fmt.Println(name, school)
}
答案1
得分: 2
每个参数都是函数内部的局部副本。你必须传递一个指向TakeInput()
的指针(例如&name
和&school
),并修改指向的值(例如*value = scanner.Text()
),否则你只会修改被函数返回时丢弃的副本。
例如:
func TakeInput(value *string) {
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
*value = scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Println("Error encountered:", err)
}
}
func main() {
var name, school string
fmt.Printf("Enter your name: ")
TakeInput(&name)
fmt.Printf("Enter your school name: ")
TakeInput(&school)
fmt.Println(name, school)
}
还要注意,bufio.Scanner
有一个内部缓冲区。它可能读取比返回的更多内容,当你在另一个TakeInput()
调用中创建一个新的bufio.Scanner
时,它可能无法读取先前读取、缓冲和丢弃的数据。
因此,在TakeInput()
之外创建扫描器,例如:
func TakeInput(scanner *bufio.Scanner, value *string) {
if scanner.Scan() {
*value = scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Println("Error encountered:", err)
}
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
var name, school string
fmt.Printf("Enter your name: ")
TakeInput(scanner, &name)
fmt.Printf("Enter your school name: ")
TakeInput(scanner, &school)
fmt.Println(name, school)
}
参考/类似的问题请参见:
英文:
Each parameter is a local copy inside the function. You must pass a pointer to TakeInput()
(e.g. &name
and &school
) and modify the pointed value (e.g. *value = scanner.Text()
, else you only modify the copy which is discarded upon function return.
For example:
func TakeInput(value *string) {
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
*value = scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Println("Error encountered:", err)
}
}
func main() {
var name, school string
fmt.Printf("Enter your name: ")
TakeInput(&name)
fmt.Printf("Enter your school name: ")
TakeInput(&school)
fmt.Println(name, school)
}
Also note that bufio.Scanner
has an internal buffer. It may read more than what is returned, which when you create a new bufio.Scanner
in another TakeInput()
call, it may not be able to read previously read, buffered and discarded data.
So create the scanner outside of TakeInput()
, e.g.
func TakeInput(scanner *bufio.Scanner, value *string) {
if scanner.Scan() {
*value = scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Println("Error encountered:", err)
}
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
var name, school string
fmt.Printf("Enter your name: ")
TakeInput(scanner, &name)
fmt.Printf("Enter your school name: ")
TakeInput(scanner, &school)
fmt.Println(name, school)
}
See related / similar questions:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论