Added existing files
This commit is contained in:
parent
3c5b2a6613
commit
6afce5b117
56
README
Normal file
56
README
Normal file
@ -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")
|
||||||
|
}
|
||||||
|
|
||||||
|
----------------------------------------------
|
19
call-stack.go
Normal file
19
call-stack.go
Normal file
@ -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
|
||||||
|
}
|
||||||
|
|
46
conv.go
Normal file
46
conv.go
Normal file
@ -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
|
||||||
|
}
|
56
maps.go
Normal file
56
maps.go
Normal file
@ -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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user