dougong5285 2014-09-03 18:44
浏览 49


Go has very nice io.Reader and io.Writer interfaces, that roughly correspond to and classes in Java (i.e. io.Reader = stream of bytes, io.Writer = sink for bytes).

I'm wondering, if Go also has some equivalent of (stream of characters) and (sink for characters) classes.

What I want is to read/write string from/to stream, without thinking about the encoding all the time + support for different encodings than just UTF8 (which is what combination of io.Reader + string typecasting gives me almost for free).

Update: so I guess my description is confusing, and I should have avoided comparison to Java stuff. What I want is something like this:

Reader part, read next 128 bytes, and return Go string by decoding bytes using ISO-8859-2 charset.

stringReader := NewStringReader(reader, "iso-8859-2")

Writer part, convert stuff to UTF16-BE bytes, and write it to the writer:

stringWriter := NewStringWriter(writer, "utf16be")
stringWriter.write("馞鮂 擙樲橚 褗褆諓");
  • 写回答

1条回答 默认 最新

  • dongpo4197 2014-09-03 19:23

    I'm not familiar enough with Java but wouldn't this do the same thing:

    // w = io.Writer
    // r = io.Reader
    sc := bufio.NewScanner(r)
    for scanner.Scan() {

    strings are a readonly []byte more or less.


    After reading the comments, I think you're looking for

    r, err := charset.NewReader("latin1", r)
    if err != nil {
    result, err := ioutil.ReadAll(r)
    if err != nil {
    ", result)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗