
こんにちは。よっしーです(^^)
本日は、Go言語のよくある質問 について解説しています。
背景
Go言語を学んでいると「なんでこんな仕様になっているんだろう?」「他の言語と違うのはなぜ?」といった疑問が湧いてきませんか。Go言語の公式サイトにあるFAQページには、そんな疑問に対する開発チームからの丁寧な回答がたくさん載っているんです。ただ、英語で書かれているため読むのに少しハードルがあるのも事実で、今回はこのFAQを日本語に翻訳して、Go言語への理解を深めていけたらと思い、これを読んだ時の内容を備忘として残しました。
型
T1とT2が同じ基底型を持つ場合、[]T1を[]T2に変換できますか?
このコードサンプルの最後の行はコンパイルされません。
type T1 int
type T2 int
var t1 T1
var x = T2(t1) // OK
var st1 []T1
var sx = ([]T2)(st1) // NOT OK
Goでは、型はメソッドと密接に結び付いており、すべての名前付き型は(おそらく空の)メソッドセットを持ちます。一般的なルールは、変換される型の名前を変更することは可能(したがってそのメソッドセットを変更する可能性がある)ですが、複合型の要素の名前(およびメソッドセット)を変更することはできません。Goでは型変換について明示的であることが求められます。
解説
この節では、Go言語における同じ基底型を持つスライス間の型変換の制限について説明されています。これは型システムとメソッドセットの関係に基づく重要な制限です。
基本型の変換 vs スライスの変換
個別要素の変換は可能
type T1 int
type T2 int
func demonstrateBasicConversion() {
// 基本型同士の変換は可能
var t1 T1 = 42
var t2 T2 = T2(t1) // OK: 明示的な型変換
fmt.Printf("t1: %v (type: %T)\n", t1, t1)
fmt.Printf("t2: %v (type: %T)\n", t2, t2)
// 双方向の変換も可能
var t3 T1 = T1(t2) // OK
fmt.Printf("t3: %v (type: %T)\n", t3, t3)
}
スライスの直接変換は不可能
func demonstrateSliceConversionError() {
var st1 []T1 = []T1{1, 2, 3, 4}
// 以下はコンパイルエラー
// var sx []T2 = ([]T2)(st1)
// cannot convert st1 (type []T1) to type []T2
// var sy []T2 = st1
// cannot use st1 (type []T1) as type []T2 in assignment
fmt.Printf("st1: %v (type: %T)\n", st1, st1)
}
メソッドセットが型変換に与える影響
異なるメソッドセットを持つ型
type Distance int
type Weight int
// Distance 型に特有のメソッド
func (d Distance) Kilometers() float64 {
return float64(d) / 1000
}
func (d Distance) Miles() float64 {
return float64(d) * 0.000621371
}
// Weight 型に特有のメソッド
func (w Weight) Kilograms() float64 {
return float64(w) / 1000
}
func (w Weight) Pounds() float64 {
return float64(w) * 0.00220462
}
func demonstrateMethodSets() {
distance := Distance(5000) // 5000メートル
weight := Weight(2000) // 2000グラム
fmt.Printf("Distance: %.2f km, %.2f miles\n", distance.Kilometers(), distance.Miles())
fmt.Printf("Weight: %.2f kg, %.2f pounds\n", weight.Kilograms(), weight.Pounds())
// 基本値の変換は可能だが、メソッドセットは失われる
convertedWeight := Weight(distance) // 値のみ変換
fmt.Printf("Converted weight: %.2f kg\n", convertedWeight.Kilograms())
// しかし、スライスの直接変換は不可能
distances := []Distance{1000, 2000, 3000}
// weights := ([]Weight)(distances) // コンパイルエラー
fmt.Printf("Distances: %v\n", distances)
}
明示的変換が必要な理由
型安全性の保証
type UserID int
type ProductID int
// UserID に特有のメソッド
func (uid UserID) IsValid() bool {
return uid > 0 && uid <= 1000000
}
// ProductID に特有のメソッド
func (pid ProductID) IsValid() bool {
return pid > 0 && pid <= 9999999
}
func demonstrateTypeSafety() {
userIDs := []UserID{1, 2, 3, 4, 5}
productIDs := []ProductID{101, 102, 103}
// 意図しない代入を防ぐ
// productIDs = userIDs // コンパイルエラー(これは良いこと!)
// 明示的な変換で意図を明確にする
convertedProductIDs := make([]ProductID, len(userIDs))
for i, uid := range userIDs {
convertedProductIDs[i] = ProductID(uid) // 明示的な変換
}
fmt.Printf("User IDs: %v\n", userIDs)
fmt.Printf("Converted to Product IDs: %v\n", convertedProductIDs)
// 各IDの有効性をチェック
for _, uid := range userIDs {
fmt.Printf("UserID %d valid: %t\n", uid, uid.IsValid())
}
for _, pid := range convertedProductIDs {
fmt.Printf("ProductID %d valid: %t\n", pid, pid.IsValid())
}
}
正しいスライス変換の方法
個別要素を変換する標準的な方法
func convertSliceT1ToT2(source []T1) []T2 {
result := make([]T2, len(source))
for i, v := range source {
result[i] = T2(v) // 各要素を明示的に変換
}
return result
}
func convertSliceT2ToT1(source []T2) []T1 {
result := make([]T1, len(source))
for i, v := range source {
result[i] = T1(v) // 各要素を明示的に変換
}
return result
}
func demonstrateCorrectSliceConversion() {
original := []T1{10, 20, 30, 40, 50}
// T1 から T2 への変換
converted := convertSliceT1ToT2(original)
fmt.Printf("Original (T1): %v\n", original)
fmt.Printf("Converted (T2): %v\n", converted)
// T2 から T1 への逆変換
reconverted := convertSliceT2ToT1(converted)
fmt.Printf("Reconverted (T1): %v\n", reconverted)
}
ジェネリクスを使った汎用変換
// Go 1.18以降のジェネリクスを使用
func ConvertSlice[T1, T2 any](source []T1, converter func(T1) T2) []T2 {
result := make([]T2, len(source))
for i, v := range source {
result[i] = converter(v)
}
return result
}
func demonstrateGenericSliceConversion() {
distances := []Distance{1000, 2000, 3000}
// Distance から Weight への変換
weights := ConvertSlice(distances, func(d Distance) Weight {
return Weight(d) // 明示的な変換関数
})
fmt.Printf("Distances: %v\n", distances)
fmt.Printf("Weights: %v\n", weights)
// より複雑な変換も可能
kilometers := ConvertSlice(distances, func(d Distance) float64 {
return d.Kilometers()
})
fmt.Printf("Kilometers: %v\n", kilometers)
}
実用的な例:異なる単位系
型安全な単位変換
type Celsius float64
type Fahrenheit float64
type Kelvin float64
func (c Celsius) ToFahrenheit() Fahrenheit {
return Fahrenheit(c*9.0/5.0 + 32.0)
}
func (c Celsius) ToKelvin() Kelvin {
return Kelvin(c + 273.15)
}
func (f Fahrenheit) ToCelsius() Celsius {
return Celsius((f - 32.0) * 5.0 / 9.0)
}
func (k Kelvin) ToCelsius() Celsius {
return Celsius(k - 273.15)
}
func convertTemperatureSlices() {
celsiusTemps := []Celsius{0, 25, 100} // 氷点、室温、沸点
// Celsius から Fahrenheit への変換
fahrenheitTemps := make([]Fahrenheit, len(celsiusTemps))
for i, temp := range celsiusTemps {
fahrenheitTemps[i] = temp.ToFahrenheit()
}
// Celsius から Kelvin への変換
kelvinTemps := make([]Kelvin, len(celsiusTemps))
for i, temp := range celsiusTemps {
kelvinTemps[i] = temp.ToKelvin()
}
fmt.Println("Temperature conversions:")
for i, c := range celsiusTemps {
fmt.Printf("%.1f°C = %.1f°F = %.1fK\n",
float64(c), float64(fahrenheitTemps[i]), float64(kelvinTemps[i]))
}
}
パフォーマンス上の考慮事項
変換のコスト
func benchmarkSliceConversion(b *testing.B) {
large := make([]T1, 1000000)
for i := range large {
large[i] = T1(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
converted := make([]T2, len(large))
for j, v := range large {
converted[j] = T2(v)
}
}
}
// unsafe パッケージを使った高速変換(注意深く使用)
func unsafeConvertSlice(source []T1) []T2 {
// 警告: これは型安全性を破る可能性がある
// T1 と T2 が同じメモリレイアウトを持つ場合のみ安全
return *(*[]T2)(unsafe.Pointer(&source))
}
func demonstrateUnsafeConversion() {
fmt.Println("警告: unsafe 変換は慎重に使用してください")
source := []T1{1, 2, 3, 4, 5}
converted := unsafeConvertSlice(source)
fmt.Printf("Source: %v (type: %T)\n", source, source)
fmt.Printf("Converted: %v (type: %T)\n", converted, converted)
// 元のスライスを変更すると変換後も影響を受ける
source[0] = 999
fmt.Printf("After modification - Source: %v\n", source)
fmt.Printf("After modification - Converted: %v\n", converted)
}
設計上の推奨事項
適切な型設計
// より良いアプローチ: 共通のインターフェースを使用
type Measurable interface {
Value() float64
Unit() string
}
type Length float64
type Mass float64
func (l Length) Value() float64 { return float64(l) }
func (l Length) Unit() string { return "meters" }
func (m Mass) Value() float64 { return float64(m) }
func (m Mass) Unit() string { return "kilograms" }
func processMeasurements(measurements []Measurable) {
for i, m := range measurements {
fmt.Printf("Measurement %d: %.2f %s\n", i, m.Value(), m.Unit())
}
}
func demonstrateBetterDesign() {
// 異なる型を同じインターフェーススライスで扱う
measurements := []Measurable{
Length(1.5),
Mass(2.3),
Length(0.8),
Mass(1.2),
}
processMeasurements(measurements)
}
この制限により、Go言語は型安全性を保ち、意図しない型変換によるバグを防ぎながら、開発者に明示的な変換を通じて意図を明確にすることを求めています。これにより、コードの可読性と保守性が向上します。
おわりに
本日は、Go言語のよくある質問について解説しました。

何か質問や相談があれば、コメントをお願いします。また、エンジニア案件の相談にも随時対応していますので、お気軽にお問い合わせください。
それでは、また明日お会いしましょう(^^)
コメント