Help us understand the problem. What is going on with this article?

goで絵文字表示可能なエクセルで開けるcsvファイルの出力(utf16le, タブ区切り)

More than 3 years have passed since last update.

UTF16LEのBOM付きでエンコード、タブ区切りで出力。

main.go
package main

import (
    "fmt"
    "encoding/csv"
    "bytes"
    "io/ioutil"
    "golang.org/x/text/transform"
    "golang.org/x/text/encoding/unicode"
    "os"
)

func main() {
    buf := new(bytes.Buffer)
    w := csv.NewWriter(buf)
    w.Comma = '\t'
    w.Write([]string{"name", "type"})
    w.Write([]string{"blacksmith🔨", "villager"})
    w.Write([]string{"creeper💣", "enemy"})
    w.Write([]string{"zonbie😡", "enemy"})
    w.Flush()
    utf16byte, err := ioutil.ReadAll(transform.NewReader(
        buf,
        unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewEncoder()))
    if err != nil {
        fmt.Println(err)
    }
    file, err := os.OpenFile("utf16le.csv", os.O_WRONLY|os.O_CREATE, 0600)
    file.Write(utf16byte)
}
sgswtky
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away