Senior
How else can the same logic be described for different types besides generics?
sobes.tech AI
Answer from AI
Besides using generics, in Go you can describe the same logic for different types using interfaces. Interfaces allow you to define a set of methods that types must implement, and work with them through the interface, providing polymorphism.
Example:
package main
import "fmt"
type Printer interface {
Print()
}
type IntPrinter int
func (i IntPrinter) Print() {
fmt.Println("Int value:", i)
}
type StringPrinter string
func (s StringPrinter) Print() {
fmt.Println("String value:", s)
}
func printValue(p Printer) {
p.Print()
}
func main() {
var i IntPrinter = 10
var s StringPrinter = "hello"
printValue(i)
printValue(s)
}
Thus, you can implement common logic through interfaces without generics.