diff --git a/README b/README new file mode 100644 index 0000000..5bd6cd0 --- /dev/null +++ b/README @@ -0,0 +1,56 @@ +Eccezioni stile Java in GO. + - Blog panic & recover: https://go.dev/blog/defer-panic-and-recover + - Libreria completa: https://hackthology.com/exceptions-for-go-as-a-library.html + - Implementazione semplificata: https://hackthology.com/exceptions-for-go-as-a-library.html + +package main + +import ( + "fmt" +) + +type Block struct { + Try func() + Catch func(Exception) + Finally func() +} + +type Exception interface{} + +func Throw(up Exception) { + panic(up) +} + +func (tcf Block) Do() { + if tcf.Finally != nil { + + defer tcf.Finally() + } + if tcf.Catch != nil { + defer func() { + if r := recover(); r != nil { + tcf.Catch(r) + } + }() + } + tcf.Try() +} + +func main() { + fmt.Println("application started") + Block { + Try: func() { + fmt.Println("this is good") + Throw("Oh,no...!!") + }, + Catch: func(e Exception) { + fmt.Printf("Caught %v\n", e) + }, + Finally: func() { + fmt.Println("finally keep runs") + }, + }.Do() + fmt.Println("application shutdown") +} + +---------------------------------------------- diff --git a/call-stack.go b/call-stack.go new file mode 100644 index 0000000..0e4e62c --- /dev/null +++ b/call-stack.go @@ -0,0 +1,19 @@ +// call-stack.go +package golang + +import ( + "runtime" +) + +func Trace(upStack int) (source string, line int, function string) { + pc := make([]uintptr, 15) + n := runtime.Callers(upStack, pc) + frames := runtime.CallersFrames(pc[:n]) + frame, _ := frames.Next() + source = frame.File + line = frame.Line + function = frame.Function + // fmt.Printf("%s:%d %s\n", frame.File, frame.Line, frame.Function) + return +} + diff --git a/conv.go b/conv.go new file mode 100644 index 0000000..36ebe21 --- /dev/null +++ b/conv.go @@ -0,0 +1,46 @@ +// conv.go +package golang + +import ( + "time" +) + +func Any2Str(v any) (s string) { + if v != nil { + // s = fmt.Sprintf("%v", v) + s, _ = v.(string) + } + return +} + +func Any2Ui64(v any, def uint64) (u uint64) { + var ok bool + if v != nil { + u, ok = v.(uint64) + } + + if !ok { + u = def + } + return +} + +func Any2I64(v any, def int64) (i int64) { + var ok bool + if v != nil { + i, ok = v.(int64) + } + + if !ok { + // err = errors.New("nil value or not int64") + i = def + } + return +} + +func Any2Time(v any) (t time.Time) { + if v != nil { + t, _ = v.(time.Time) + } + return +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d7dd0ca --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module portale-stac.it/packages/golang + +go 1.21.6 diff --git a/maps.go b/maps.go new file mode 100644 index 0000000..3f06581 --- /dev/null +++ b/maps.go @@ -0,0 +1,56 @@ +// maps.go +package golang + +import ( + "cmp" + "slices" +) + +func MapKeys[K cmp.Ordered, V any](m map[K]V, sort bool) (keys []K) { + keys = make([]K, len(m)) + i := 0 + for key := range m { + keys[i] = key + i++ + } + if sort { + slices.Sort(keys) + } + return keys +} + +func IterateSortedMap[K cmp.Ordered, V any](m map[K]V, op func(key K, value V) error) (err error) { + keys := MapKeys(m, true) + for _, key := range keys { + if err = op(key, m[key]); err != nil { + break + } + } + return +} + +func IterateMap[K cmp.Ordered, V any](m map[K]V, op func(key K, value V) error) (err error) { + for key, value := range m { + if err = op(key, value); err != nil { + break + } + } + return +} + +func MapListGet[K cmp.Ordered, V any](key K, maps ...map[K]V) (value V, mapIndex int) { + return MapListGetV(key, maps) +} + +func MapListGetV[K cmp.Ordered, V any](key K, maps []map[K]V) (value V, mapIndex int) { + var ok bool + mapIndex = -1 + for index, m := range maps { + if value, ok = m[key]; ok { + mapIndex = index + break + } + } + return +} +