Discussion:
fmt.Scan expects runes to be integers?
2***@public.gmane.org
2012-11-15 22:11:11 UTC
Permalink
Anybody know why? Reading characters would be pretty useful.

var r rune
_, err := fmt.Scan(&r)
if err != nil {
fmt.Println(err)
}

$ go run asdf.go
a
expected integer

--
gauravk
2012-11-15 23:05:53 UTC
Permalink
I think you need to do this:

var r rune
_, err := fmt.Scanf("%c", &r)
// _, err := fmt.Scan(&r)
if err != nil {
fmt.Println(err)
}
Post by 2***@public.gmane.org
Anybody know why? Reading characters would be pretty useful.
var r rune
_, err := fmt.Scan(&r)
if err != nil {
fmt.Println(err)
}
$ go run asdf.go
a
expected integer
--
2***@public.gmane.org
2012-11-15 23:17:17 UTC
Permalink
Right, thanks. But I'm wondering if this is the correct choice of interface
for Scan. Seems like reading characters is a lot more common than reading
integers as runes. And I think it's very natural to expect the character
behavior.
Post by 2***@public.gmane.org
var r rune
_, err := fmt.Scanf("%c", &r)
// _, err := fmt.Scan(&r)
if err != nil {
fmt.Println(err)
}
Post by 2***@public.gmane.org
Anybody know why? Reading characters would be pretty useful.
var r rune
_, err := fmt.Scan(&r)
if err != nil {
fmt.Println(err)
}
$ go run asdf.go
a
expected integer
--
Jesse McNelis
2012-11-15 23:26:37 UTC
Permalink
Post by 2***@public.gmane.org
Right, thanks. But I'm wondering if this is the correct choice of
interface for Scan. Seems like reading characters is a lot more common than
reading integers as runes. And I think it's very natural to expect the
character behavior.
A rune is 4bytes, Your 'a' is 1 byte(assuming your terminal input is ascii
or utf8).
If your terminal input in utf32 then fmt.Scanf() would work as you expect
as your 'a' would be 4bytes.
Go doesn't know about your terminal so it can't know what encoding it uses.

--
Ryan Brown
2012-11-16 00:41:51 UTC
Permalink
Ah, I didn't realize that rune is actually an alias of int32, not distinct
type.
Post by 2***@public.gmane.org
Right, thanks. But I'm wondering if this is the correct choice of
interface for Scan. Seems like reading characters is a lot more common than
reading integers as runes. And I think it's very natural to expect the
character behavior.
A rune is 4bytes, Your 'a' is 1 byte(assuming your terminal input is
ascii or utf8).
If your terminal input in utf32 then fmt.Scanf() would work as you expect
as your 'a' would be 4bytes.
Go doesn't know about your terminal so it can't know what encoding it uses.
--
Ryan Brown
2012-11-16 00:19:44 UTC
Permalink
If I change the type to string, I'm able to read it. So what you're telling
me is that the purpose of rune is not to represent a single character of a
go string, but instead to represent a character of a string of any encoding
(that has 4-byte or less characters)? And when I use %c I'm saying, "hey,
this rune is actually a go string character".
Post by 2***@public.gmane.org
Right, thanks. But I'm wondering if this is the correct choice of
interface for Scan. Seems like reading characters is a lot more common than
reading integers as runes. And I think it's very natural to expect the
character behavior.
A rune is 4bytes, Your 'a' is 1 byte(assuming your terminal input is
ascii or utf8).
If your terminal input in utf32 then fmt.Scanf() would work as you expect
as your 'a' would be 4bytes.
Go doesn't know about your terminal so it can't know what encoding it uses.
--

Loading...