Strings with blank convert to bytes

74 views
Skip to first unread message

洪嘉鴻

unread,
Jul 9, 2020, 10:03:26 PM7/9/20
to golang-nuts
Hello everyone:
I use golang with Win10. The version of golang I used is go1.12.9.
This is a simple code I'm trying to figure out.
However, I have no idea how to edit to make the output "b1" the same as "b" if "b1" is read from user input.
Could anyone help me to solve this problem?

Any help is appreciated.
Thank you very much!
Max

tokers

unread,
Jul 9, 2020, 10:12:04 PM7/9/20
to golang-nuts
Hello!

Just quote the document:

> Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format

Instead, you can use fmt.Scanln.

洪嘉鴻

unread,
Jul 9, 2020, 10:23:28 PM7/9/20
to golang-nuts
Hello!
The problem isn't solve when I use "fmt.Scanln" instead of "fmt.Scanf".
That is, the result is the same whatever I use "fmt.Scanln" or "fmt.Scanf".

Thanks for your replying.
Max

tokers於 2020年7月10日星期五 UTC+8上午10時12分04秒寫道:

Kurtis Rader

unread,
Jul 9, 2020, 11:03:51 PM7/9/20
to 洪嘉鴻, golang-nuts
On Thu, Jul 9, 2020 at 7:23 PM 洪嘉鴻 <max1...@gmail.com> wrote:
Hello!
The problem isn't solve when I use "fmt.Scanln" instead of "fmt.Scanf".
That is, the result is the same whatever I use "fmt.Scanln" or "fmt.Scanf".

From the documentation:

the %s verb (and %v reading into a string) stops consuming input at the first space or newline character.

You shouldn't be using the Scanf family of functions if you don't want the input split on whitespace boundaries. You probably want bufio.ReadLine or similar. See https://stackoverflow.com/questions/8757389/reading-a-file-line-by-line-in-go
 
Instead, you can use fmt.Scanln.
On Friday, July 10, 2020 at 10:03:26 AM UTC+8 max1...@gmail.com wrote:
Hello everyone:
I use golang with Win10. The version of golang I used is go1.12.9.
This is a simple code I'm trying to figure out.
However, I have no idea how to edit to make the output "b1" the same as "b" if "b1" is read from user input.
Could anyone help me to solve this problem?

Any help is appreciated.
Thank you very much!
Max

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5d33d2f0-6043-44cc-8c6a-6da4f9cd1dc0o%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

洪嘉鴻

unread,
Jul 9, 2020, 11:41:01 PM7/9/20
to golang-nuts
Hello!
I've just found this according the information which you provided.
Although there is a little different that it also reads "\n" with ReadString().
However, it solves my problem.

Thanks for your replying.
Max

Kurtis Rader於 2020年7月10日星期五 UTC+8上午11時03分51秒寫道:
On Thu, Jul 9, 2020 at 7:23 PM 洪嘉鴻 <max1...@gmail.com> wrote:
Hello!
The problem isn't solve when I use "fmt.Scanln" instead of "fmt.Scanf".
That is, the result is the same whatever I use "fmt.Scanln" or "fmt.Scanf".

From the documentation:

the %s verb (and %v reading into a string) stops consuming input at the first space or newline character.

You shouldn't be using the Scanf family of functions if you don't want the input split on whitespace boundaries. You probably want bufio.ReadLine or similar. See https://stackoverflow.com/questions/8757389/reading-a-file-line-by-line-in-go
 
Instead, you can use fmt.Scanln.
On Friday, July 10, 2020 at 10:03:26 AM UTC+8 max1...@gmail.com wrote:
Hello everyone:
I use golang with Win10. The version of golang I used is go1.12.9.
This is a simple code I'm trying to figure out.
However, I have no idea how to edit to make the output "b1" the same as "b" if "b1" is read from user input.
Could anyone help me to solve this problem?

Any help is appreciated.
Thank you very much!
Max

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages