From 6da4edff070b5e8523243560e03bb77999c595d8 Mon Sep 17 00:00:00 2001 From: Archie Fox Date: Fri, 14 Mar 2025 17:50:02 +0300 Subject: [PATCH] Add generic and generic structs --- go-demo-4/main.go | 3 +++ go-demo-4/output/errors.go | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/go-demo-4/main.go b/go-demo-4/main.go index 598fd88..13b8aea 100644 --- a/go-demo-4/main.go +++ b/go-demo-4/main.go @@ -10,6 +10,9 @@ import ( ) func main() { + // fmt.Println(output.Sum(100, 42)) + // fmt.Println(output.Sum(10.7, 3.14)) + // fmt.Println(output.Sum("Hello ", "world!")) fmt.Println("__Менеджер паролей__") vault := account.NewVault(files.NewJsonDb("data.json")) Menu: diff --git a/go-demo-4/output/errors.go b/go-demo-4/output/errors.go index 1414dfc..4c72d6e 100644 --- a/go-demo-4/output/errors.go +++ b/go-demo-4/output/errors.go @@ -14,3 +14,13 @@ func PrintError(value any) { color.Red("Неизвестный тип ошибки") } } + +// Generic +func Sum[T int | float32 | float64 | string](a, b T) T { + return a + b +} + +// Generic structs +type List[T any] struct { + elements []T +}