Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: add *input* builtin function #40194

Closed
dotoscat opened this issue Jul 13, 2020 · 10 comments
Closed

proposal: add *input* builtin function #40194

dotoscat opened this issue Jul 13, 2020 · 10 comments

Comments

@dotoscat
Copy link

Hello. I don't know whether this is the correct place for this proposal.

input would be a convenience function for a quick interaction with the user from the command line, similar to Python's

I think its implementation could be this simple:

func input(prompt string) (string, error) {
	fmt.Print(prompt)
	reader := bufio.NewReader(os.Stdin)
	response, err := reader.ReadString('\n')
	return response, err
}

Example of use

func main() {
    age, err := input("Tell me your age: ")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("So your age is", age)
}
@gopherbot gopherbot added this to the Proposal milestone Jul 13, 2020
@ianlancetaylor
Copy link
Contributor

Thanks. This seems like a function that could easily live in a third party library. The standard library doesn't have any package or set of functions for interactive I/O. https://golang.org/doc/faq#x_in_std

(Note that your suggested implementation is not safe, as the buffered os.Stdin may read additional characters past the newline, and those characters will be lost when the function returns.)

@dotoscat
Copy link
Author

Thank you very much for your response. Im already working on a module with the fixes (I hope).

@davecheney
Copy link
Contributor

Closing as declined.

@ianlancetaylor
Copy link
Contributor

@davecheney To be clear I didn't mean my comment to decline the proposal entirely. I was raising an objection. If @dotoscat wants to respond we can reopen this.

@davecheney
Copy link
Contributor

My mistake, I interpreted #40194 (comment) as @dotoscat had moved on to implementing this in their own package.

@dotoscat
Copy link
Author

After some tries with Scan, Scanf and Scanln I come to the conclussion to use reader := bufio.NewReader(os.Stdin) as a global.

Something like this

var bufferedStdin *bufio.Reader = bufio.NewReader(os.Stdin)

which works if os.Stdin is the output from another program and is harmless if os.Stdin is a tty.

cat TODO | ./input

So the implementation of input would be

func input(prompt string) (string, error) {
	fmt.Print(prompt)
	response, err := bufferedStdin.ReadString('\n')
	trimmedResponse := strings.TrimRight(response, "\n")
	return trimmedResponse, err
}

And excuse me for this, there is a problem that happens with string.TrimRight though

func main() {
	fruit, _ := input("Favorite fruit? ")
	things, _ := input("Favorite things? ")
	number, _ := input("Favorite number? ")
	fmt.Println(fruit)
	fmt.Println(things)
	fmt.Println(number)

	fmt.Println("Your favorite things", fruit, things, number) //here

	fmt.Println("END")
}

Get-Content stuff.txt | ./input (I'm using Windows 10)

The output is

false
Favorite fruit? Favorite things? Favorite number? apple
pretty things
7
 7retty thingsthings apple  <----
END

The output is being stepped on, which don't happen without strings.TrimRight or any other strings functions

I attach the files for the reproducible example.

goinput.zip

@ianlancetaylor
Copy link
Contributor

@dotoscat Do you still want to propose a change to the standard library?

@dotoscat
Copy link
Author

dotoscat commented Jul 15, 2020 via email

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Jul 15, 2020
@ianlancetaylor
Copy link
Contributor

Reopened.

Please do consider https://golang.org/doc/faq#x_in_std . Why does the proposed function belong in the standard library? Thanks.

@dotoscat
Copy link
Author

After thinking about it carefully I don't see any reason to the proposed function be part of the standard library. Thank you for your attention.

@martisch martisch removed this from Incoming in Proposals (old) Aug 14, 2020
@golang golang locked and limited conversation to collaborators Jul 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants