How to read an UTF-8 file into a string?

770 views
Skip to first unread message

ceving

unread,
Apr 14, 2010, 5:29:43 AM4/14/10
to golang-nuts
Hi,

is there an example how to do that?

And the next question is: how to work on the string by characters
instead of bytes. For example: how to crop the first unicode character
from a string?

Regards

chris dollin

unread,
Apr 14, 2010, 5:34:55 AM4/14/10
to ceving, golang-nuts
On 14 April 2010 10:29, ceving <cev...@googlemail.com> wrote:
Hi,

is there an example how to do that?

I would read the file into a byte array / buffer and then convert to
string. If the file has UTF8 byte encoding in it, that should just
work.
 
And the next question is: how to work on the string by characters
instead of bytes. For example: how to crop the first unicode character
from a string?
 
You can iterate over the codepoints using for ... range. But if you
want to change things, that won't help.

If you're going to be fiddling around with the codepoints in a non-sequential
way, you probably want a different datatype than string.

Chris

--
Chris "allusive" Dollin
Reply all
Reply to author
Forward
0 new messages