Skip to content

Instantly share code, notes, and snippets.

@heiwa4126
Last active February 11, 2024 10:36
Show Gist options
  • Save heiwa4126/c7a43913fde7c02c5385d0247b13bc6e to your computer and use it in GitHub Desktop.
Save heiwa4126/c7a43913fde7c02c5385d0247b13bc6e to your computer and use it in GitHub Desktop.
GolangでUTF8をShiftJISに変換する基本。
package main
import (
"fmt"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
"log"
)
func main() {
s := "世界の皆さん、こんにちは!"
t := japanese.ShiftJIS.NewEncoder()
sjisStr, _, err := transform.String(t, s)
if err != nil {
log.Fatal(err)
}
fmt.Println([]byte(sjisStr))
}
@heiwa4126
Copy link
Author

heiwa4126 commented Mar 6, 2020

The Go Playground
ふつうは

sjis, _, err := transform.Bytes(t, []byte(s))

とかだと思う。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment