LoginSignup
0
0

Goのジェネリクス

Posted at

ジェネリクスとは

Golang1.18で導入された機能。
型の安全性は保ちつつ、異なる型を引数で取りうるといったものです。
コードの再利用性の向上が期待できます。

main.go
package main

import "fmt"

// 型パラメータを持つ関数
func Print[T any](s []T) {
    for _, v := range s {
        fmt.Println(v)
    }
}

func main() {
    Print([]int{1, 2, 3})
    Print([]string{"a", "b", "c"})
}

型パラメータとしてTを利用しています。

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