LoginSignup
0
0

golangでmap関数っぽいもの

Last updated at Posted at 2023-08-12

何度も同じコードを書くので備忘録として。

// mapArray T型配列をR型配列に変換する
func mapArray[T any, R any](values []T, conv func(t T) R) []R {
	results := make([]R, len(values))
	for i, v := range values {
		results[i] = conv(v)
	}
	return results
}

使い方

intValues := []int{1, 2, 3, 4}
strValues = mapArray(intValues, func(v int) string {
		return strconv.FormatInt(int64(v), 10)
	})
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