Re: fmt.Scan expects runes to be integers?

228 views
Skip to first unread message

gauravk

unread,
Nov 15, 2012, 6:05:53 PM11/15/12
to golan...@googlegroups.com, 22r...@gmail.com
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)
        }

On Thursday, November 15, 2012 5:11:11 PM UTC-5, 22r...@gmail.com wrote:
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

unread,
Nov 15, 2012, 6:26:37 PM11/15/12
to 22r...@gmail.com, golang-nuts
On Fri, Nov 16, 2012 at 10:17 AM, <22r...@gmail.com> wrote:
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.

Reply all
Reply to author
Forward
0 new messages