0
0

More than 1 year has passed since last update.

[Go] 型変換 & 型アサーション の使い方がすぐに思い出せない場合は、こちらから公式サンプルページへ

Posted at

型変換 (Type Conversion)

変数v, 型T があった場合、T(v) は 変数v を T型へ変換します。

i := 42         // int

// 型(変数) で型変換
f := float64(i) // float
u := uint(f)    // uint

A Tour of Go のページ

The Go Programming Language Specification のページ

型アサーション (Type Assertion)

インターフェース型i を 型T に変換

// t, ok := i.(T)

var i interface{} = "hello"

s, ok := i.(string) // s は string 型 で "hello" を保持
fmt.Println(s, ok)

A Tour of Go のページ

Effective Go のページ

The Go Programming Language Specification のページ

ちなみに

調べて分かったことは、「型キャスト (Type Casting)」という言葉は、公式には使われていないようです。
検索に引っかかりませんでした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0