fmt.Fscan without delimeter

87 views
Skip to first unread message

Brian Picciano

unread,
Oct 19, 2016, 10:34:35 AM10/19/16
to golang-nuts
Hi there! My use-case involves reading all data off of an io.Reader and scanning it into a receiver value provided by the user of my library. In many ways the same thing as fmt.Fscan. The difference is that only one receiver value is allowed, and I want to read _all_ data until io.EOF, not just until space or newline. So if my data is "hello world" and I'm scanning into a string, that string should have "hello world" in it. If my data is "12 OK" and I'm scanning into an int, that should error because the entirety of the data is not a parseable integer.

If there was a fmt.Scanf which simply had no delimiter, or where the delimiter was somehow set to io.EOF, I think that would work for what I'm doing. But I can't find anything like that. Does anyone know of anything that could help me do this? Or do I just have to implement it myself?

Thanks!
Brian

Ian Davis

unread,
Oct 19, 2016, 10:38:58 AM10/19/16
to golan...@googlegroups.com

On Wed, Oct 19, 2016, at 03:34 PM, Brian Picciano wrote:
Hi there! My use-case involves reading all data off of an io.Reader and scanning it into a receiver value provided by the user of my library. In many ways the same thing as fmt.Fscan. The difference is that only one receiver value is allowed, and I want to read _all_ data until io.EOF, not just until space or newline. So if my data is "hello world" and I'm scanning into a string, that string should have "hello world" in it. If my data is "12 OK" and I'm scanning into an int, that should error because the entirety of the data is not a parseable integer.

If there was a fmt.Scanf which simply had no delimiter, or where the delimiter was somehow set to io.EOF, I think that would work for what I'm doing. But I can't find anything like that. Does anyone know of anything that could help me do this? Or do I just have to implement it myself?

Could you write a Reader that send a delimiter in the stream when it encounters io.EOF? Then pass that reader to Fscan.

Ian

Brian Picciano

unread,
Oct 19, 2016, 11:25:22 AM10/19/16
to Ian Davis, golan...@googlegroups.com
Hi Ian! I don't think that would work, my data can be pretty much any arbitrary data, including binary data. So I would need to tell Fscan to ignore any spaces it sees in the data itself, and then somehow accept the space which I replaced from EOF.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/3_QJ6qK5Hqw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jakob Borg

unread,
Oct 19, 2016, 11:38:24 AM10/19/16
to Brian Picciano, Ian Davis, golan...@googlegroups.com
You could, perhaps, use ioutil.ReadAll and then typeswitch for the common types (strings, []byte, the numerics) that you can easily handle. If the given receiver was not of one of those types, try to cast it to an encoding.BinaryUnmarshaler and/or encoding.TextUnmarshaler since that's essentially what you're doing. Let the user implement the type specific unmarshalling. Document the expectations for the library user. :)

//jb


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.

Brian Picciano

unread,
Oct 19, 2016, 7:20:06 PM10/19/16
to Jakob Borg, Ian Davis, golan...@googlegroups.com
I'd expected I'd have to do something along those lines, wanted to double check first to make sure I wasn't missing something. Thanks!
Reply all
Reply to author
Forward
0 new messages