Go言語での文字列変換

プログラミング

go言語での文字列の他データ型への変換についてさらっと書きます。

こちらの公式ドキュメントに言及、一部和訳しています。

The Go Programming Language Specification - The Go Programming Language

go言語の文字列(string型)は、rune型のスライス([]rune)byte型のスライス([]byte)にそれぞれ変換可能です。

rune型の実態はint32で、Unicodeのコードポイントを10進数表記で表します。

// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32

下記のように for range で文字列を繰り返し処理にかけると、rune型で一文字ずつ取得できます。

str := "あいうえお"
for _, s := range str {
    fmt.Printf("Type: %s, Value: %d\n", reflect.TypeOf(s), s)
}

/* OUTPUT:
Type: int32, Value: 12354
Type: int32, Value: 12356
Type: int32, Value: 12358
Type: int32, Value: 12360
Type: int32, Value: 12362
*/
Unicode文字一覧表 - instant tools

各文字の出力がUnicodeのコードポイントの数値に合致していることがわかります。

またbyte型の実態はuint8で、1バイトを10進数表記で表します。

下記のように for 初期値; 条件; 変化式 で文字列を繰り返し処理にかけると、byte型で1バイトごとに取得できます。

str := "あいうえお"
for i := 0; i < len(str); i++ {
    fmt.Printf("Type: %s, Value: %d\n", reflect.TypeOf(str[i]), str[i])
}

/* OUTPUT:
Type: uint8, Value: 227
Type: uint8, Value: 129
Type: uint8, Value: 130
Type: uint8, Value: 227
Type: uint8, Value: 129
Type: uint8, Value: 132
Type: uint8, Value: 227
Type: uint8, Value: 129
Type: uint8, Value: 134
Type: uint8, Value: 227
Type: uint8, Value: 129
Type: uint8, Value: 136
Type: uint8, Value: 227
Type: uint8, Value: 129
Type: uint8, Value: 138
*/

このケースではバイトごとの繰り返しになるため、日本語のようなマルチバイト文字だと1文字ずつ抜き出せないことに注意してください。

rune型の場合はUnicodeコードポイントで1文字ごとに対応しているため、string(value)で文字列に変換できます。

数値をstring変換した場合は、その数値に対応するUnicodeコードポイントの文字に変換されます。
コードポイント範囲外の数値の場合は \ufffd に変換されます。

num1 := 12450
fmt.Println(string(num1))

/* OUTPUT:
ア
*/

stringを介さずにruneとbyteを変換する場合はunicode/utf8パッケージのEncodeRuneDecodeRuneなどが使えるかと思います。

utf8 package - unicode/utf8 - Go Packages
Package utf8 implements functions and constants to support text encoded in UTF-8.
この記事を書いた人
rio

英語を使う職業プログラマー。
英会話や英語学習は大好きなのに資格を取るのが面倒すぎ、最近ようやく重い腰を上げてTOEICを受験。
自身の英語力を信じてぶっつけ本番で挑み、945点取得。
プログラミングは最近ではGo言語、Typescript、Javascriptをよく書いている。

このブログでは、英会話や英語学習のトピックや、プログラミング関連の記事を執筆していきます。

rioをフォローする
プログラミング
rioをフォローする
えいごギークのほっとtime

コメント

タイトルとURLをコピーしました