title: How to read utf16 text file to string in golang?
body: <p>I can read the file to bytes array</p>
  
  <p>but when I convert it to string</p>
  
  <p>it treat the utf16 bytes as ascii</p>
  
  <p>How to convert it correctly?</p>
  
  <pre><code>package main
  
  import ("fmt"
  "os"
  "bufio"
  )
  
  func main(){
      // read whole the file
      f, err := os.Open("test.txt")
      if err != nil {
          fmt.Printf("error opening file: %v\n",err)
          os.Exit(1)
      }
      r := bufio.NewReader(f)
      var s,b,e = r.ReadLine()
      if e==nil{
          fmt.Println(b)
          fmt.Println(s)
          fmt.Println(string(s))
      }
  }
  </code></pre>
  
  <hr>
  
  <p>output:</p>
  
  <p>false</p>
  
  <p>[255 254 91 0 83 0 99 0 114 0 105 0 112 0 116 0 32 0 73 0 110 0 102 0 111 0 93 0
   13 0]</p>
  
  <p>S c r i p t I n f o ]</p>
  
  <hr>
  
  <p>Update:</p>
  
  <p>After I tested the two examples, I have understanded what is the exact problem now.</p>
  
  <p>In windows, if I add the line break (CR+LF) at the end of the line, the CR will be read in the line. Because the readline function cannot handle unicode correctly ([OD OA]=ok, [OD 00 OA 00]=not ok).</p>
  
  <p>If the readline function can recognize unicode, it should understand [OD 00 OA 00] and return []uint16 rather than []bytes.</p>
  
  <p>So I think I should not use bufio.NewReader as it is not able to read utf16, I don't see bufio.NewReader.ReadLine can accept parameter as flag to indicate the reading text is utf8, utf16le/be or utf32. Is there any readline function for unicode text in go library?</p>