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
Labels
Milestone

Comments

@dotoscat
Copy link

@dotoscat dotoscat commented Jul 13, 2020

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
@gopherbot gopherbot added the Proposal label Jul 13, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jul 13, 2020

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

@dotoscat dotoscat commented Jul 14, 2020

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

@davecheney
Copy link
Contributor

@davecheney davecheney commented Jul 15, 2020

Closing as declined.

@davecheney davecheney closed this Jul 15, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jul 15, 2020

@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

@davecheney davecheney commented Jul 15, 2020

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

@dotoscat
Copy link
Author

@dotoscat dotoscat commented Jul 15, 2020

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

@ianlancetaylor ianlancetaylor commented Jul 15, 2020

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

@dotoscat
Copy link
Author

@dotoscat dotoscat commented Jul 15, 2020

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

@ianlancetaylor ianlancetaylor commented Jul 15, 2020

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

@dotoscat dotoscat commented Jul 19, 2020

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.

@dotoscat dotoscat closed this Jul 19, 2020
@martisch martisch removed this from Incoming in Proposals Aug 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.