A generic identity function keeps one operation shape while the caller chooses the value type.

type parameter A type parameter lets one function work with more than one concrete type. The trace uses a concrete string helper and shows the generic shape in a comment.

Generic Identity

word
generic_identity.go
Replay: real traced execution (multi-file project)
package main

import "fmt"

// Generic shape: Identity[T any](value T) T
func IdentityString(value string) string {
	return value
}

func main() {
	var word = "gopher"
	result := IdentityString(word)
	length := len(result)

	fmt.Println("word=", word)
	fmt.Println("result=", result)
	fmt.Println("length=", length)
}
package main

import "fmt"

// Generic shape: Identity[T any](value T) T
func IdentityString(value string) string {
	return value
}

func main() {
	var word = "generic"
	result := IdentityString(word)
	length := len(result)

	fmt.Println("word=", word)
	fmt.Println("result=", result)
	fmt.Println("length=", length)
}
package main

import "fmt"

// Generic shape: Identity[T any](value T) T
func IdentityString(value string) string {
	return value
}

func main() {
	var word = "type"
	result := IdentityString(word)
	length := len(result)

	fmt.Println("word=", word)
	fmt.Println("result=", result)
	fmt.Println("length=", length)
}
  1. word ← "gopher"

    10func main() {11  var word→ "gopher" = "gopher" //@word="generic", "type"12  result := IdentityString(word"gopher")13  length := len(result)
  2. func IdentityString(value string) string

    5// Generic shape: Identity[T any](value T) T6func IdentityString(value"gopher" string) string {7  return value"gopher"8}
  3. result ← "gopher", length ← 6

    11  var word = "gopher" //@word="generic", "type"12  result→ "gopher" := IdentityString(word"gopher")13  length→ 6 := len(result"gopher")1415  fmt.Println("word=", word"gopher")16  fmt.Println("result=", result"gopher")17  fmt.Println("length=", length6)18}
    outputword= gopher
    result= gopher
    length= 6
  1. word ← "generic"

    10func main() {11  var word→ "generic" = "generic"12  result := IdentityString(word"generic")13  length := len(result)
  2. func IdentityString(value string) string

    5// Generic shape: Identity[T any](value T) T6func IdentityString(value"generic" string) string {7  return value"generic"8}
  3. result ← "generic", length ← 7

    11  var word = "generic"12  result→ "generic" := IdentityString(word"generic")13  length→ 7 := len(result"generic")1415  fmt.Println("word=", word"generic")16  fmt.Println("result=", result"generic")17  fmt.Println("length=", length7)18}
    outputword= generic
    result= generic
    length= 7
  1. word ← "type"

    10func main() {11  var word→ "type" = "type"12  result := IdentityString(word"type")13  length := len(result)
  2. func IdentityString(value string) string

    5// Generic shape: Identity[T any](value T) T6func IdentityString(value"type" string) string {7  return value"type"8}
  3. result ← "type", length ← 4

    11  var word = "type"12  result→ "type" := IdentityString(word"type")13  length→ 4 := len(result"type")1415  fmt.Println("word=", word"type")16  fmt.Println("result=", result"type")17  fmt.Println("length=", length4)18}
    outputword= type
    result= type
    length= 4