How to Write a Generic Result Type in Go

Use a generic Result[T] struct with an interface constraint to create a unified return type for success and error states in Go.

Define a generic function with a type parameter constrained by an interface to return a uniform result type. Use the Result[T] struct pattern to wrap success and failure states, allowing the caller to handle both cases with a single type.

type Result[T any] struct {
	Value T
	Error error
}

func Fetch[T any](id int) Result[T] {
	// Implementation logic
	return Result[T]{Value: zeroValue, Error: nil}
}