[Go] []byte 型のデータを string 型に変換する

作成日: 2025年03月15日

string([]byte) のように型を変換する構文を使用すると、[]byte 型のデータを string 型に変換することができます。下記の例では、hello という []byte 型のデータを string 型に変換し、変数 str の型を出力しています。

package main

import (
    "fmt"
    "reflect"
)

func main() {
    bytes := []byte("hello")
    str := string(bytes)
    fmt.Println(reflect.TypeOf(str))
}

実行結果は下記のとおりです。

string
Go