如何将扫描器写成一个独立的函数以使其工作?

huangapple go评论99阅读模式
英文:

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)
}

参考/类似的问题请参见:

https://stackoverflow.com/questions/43890706/my-object-is-not-updated-even-if-i-use-the-pointer-to-a-type-to-update-it/43890923#43890923

https://stackoverflow.com/questions/47435161/remove-an-element-of-a-slice-in-a-struct/47435232#47435232

英文:

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:

https://stackoverflow.com/questions/43890706/my-object-is-not-updated-even-if-i-use-the-pointer-to-a-type-to-update-it/43890923#43890923

https://stackoverflow.com/questions/47435161/remove-an-element-of-a-slice-in-a-struct/47435232#47435232

huangapple
  • 本文由 发表于 2022年9月1日 15:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/73565417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定