KDOC 430: エイリアス型に対する型アサーションを見る

この文書のステータス

  • 作成
    • 2025-09-04 貴島
  • レビュー
    • 2025-09-04 貴島

概要

Go言語で、 byte 型は uint8 のエイリアス型である。

// byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
// used, by convention, to distinguish byte values from 8-bit unsigned
// integer values.
type byte = uint8

したがって、 byte 型 を型アサーションすると、 uint8 型でも通過する。

import "fmt"
func main() {
        var x interface{}

        x = byte(1)

        // 1
        {
                _, ok := x.(byte)
                if ok {
                        fmt.Println("1 byteで型アサーション通過した")
                }
        }

        // 2
        {
                _, ok := x.(uint8)
                if ok {
                        fmt.Println("2 uint8で型アサーション通過した")
                }
        }
}
1 byteで型アサーション通過した
2 uint8で型アサーション通過した

関連

なし。

Backlinks