Reading a utf-16 text file

Hello all.
I need work with information, which written in UTF-16 file.
I’m new in Go, but heard that he is good Unicode support.
So my task is to extract the text in string for further work.
I tried to do the conversion in the forehead, but it turned out badly:

package main

import (
	utf16 "unicode/utf16"

func main() {

	file, err := os.Open("in.txt")
	if err != nil {
	defer file.Close()
	fscanner := bufio.NewScanner(file)
	for fscanner.Scan() {
		arrbytes := fscanner.Bytes()
		slice := make([]uint16, len(arrbytes))
		for i := range arrbytes {
			slice[i] = uint16(arrbytes[i])
		newarr := utf16.Decode(slice)


I would appreciate any help on this issue.

Note, put your code on so people can get the code. You can use base64 to encode the filedata if necessary. Check out package. might work, I don’t have a UTF16 file laying around to test.


thanks, this really works.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.