-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a90441
commit 2bf0727
Showing
11 changed files
with
301 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
module github.com/KevinZonda/GoX | ||
go 1.19 | ||
go 1.22 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package jsonx | ||
|
||
import "encoding/json" | ||
|
||
func ToString(o any) string { | ||
bs, err := json.Marshal(o) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return string(bs) | ||
} | ||
|
||
func ToStringE(o any) (string, error) { | ||
bs, err := json.Marshal(o) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(bs), nil | ||
} | ||
|
||
// ToStringI with indent | ||
func ToStringI(o any) string { | ||
bs, err := json.MarshalIndent(o, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
return string(bs) | ||
} | ||
|
||
// ToStringIE with indent | ||
func ToStringIE(o any) (string, error) { | ||
bs, err := json.MarshalIndent(o, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(bs), nil | ||
} | ||
|
||
func ToObj[T any](s string) T { | ||
var v T | ||
err := json.Unmarshal([]byte(s), &v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return v | ||
} | ||
|
||
func ToObjE[T any](s string) (T, error) { | ||
var v T | ||
err := json.Unmarshal([]byte(s), &v) | ||
return v, err | ||
} | ||
|
||
func ToObjPtr[T any](s string) *T { | ||
var v T | ||
err := json.Unmarshal([]byte(s), &v) | ||
if err != nil || v == nil { | ||
return nil | ||
} | ||
return &v | ||
} | ||
|
||
func ToObjPtrE[T any](s string) (*T, error) { | ||
var v T | ||
err := json.Unmarshal([]byte(s), &v) | ||
if err != nil || v == nil { | ||
return nil, err | ||
} | ||
return &v, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package lambda | ||
|
||
func Map[T any, R any](in []T, f func(T) R) []R { | ||
out := make([]R, len(in)) | ||
for i, v := range in { | ||
out[i] = f(v) | ||
} | ||
return out | ||
} | ||
|
||
func ForEach[T any](in []T, f func(T)) { | ||
for _, v := range in { | ||
f(v) | ||
} | ||
} | ||
|
||
func Filter[T any](in []T, f func(T) bool) []T { | ||
out := make([]T, 0, len(in)) | ||
for _, v := range in { | ||
if f(v) { | ||
out = append(out, v) | ||
} | ||
} | ||
return out | ||
} | ||
|
||
func Find[T any](in []T, f func(T) bool) (T, bool) { | ||
for _, v := range in { | ||
if f(v) { | ||
return v, true | ||
} | ||
} | ||
var zero T | ||
return zero, false | ||
} | ||
|
||
func Any[T any](in []T, f func(T) bool) bool { | ||
for _, v := range in { | ||
if f(v) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func All[T any](in []T, f func(T) bool) bool { | ||
for _, v := range in { | ||
if !f(v) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,7 @@ | ||
package panicx | ||
|
||
import "fmt" | ||
|
||
func PanicIf(exp bool, v any) { | ||
func If(exp bool, v any) { | ||
if exp { | ||
panic(v) | ||
} | ||
} | ||
|
||
func PanicIfNotNil(b any, v any) { | ||
if b != nil { | ||
panic(v) | ||
} | ||
} | ||
|
||
func PanicF(format string, a ...any) { | ||
panic(fmt.Sprintf(format, a...)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package stringx | ||
|
||
import "strings" | ||
|
||
func Join(sep string, ss ...string) string { | ||
return strings.Join(ss, sep) | ||
} | ||
|
||
func JoinNotEmpty(sep string, ss ...string) string { | ||
var _ss []string | ||
for _, s := range ss { | ||
if s != "" { | ||
_ss = append(_ss, s) | ||
} | ||
} | ||
return strings.Join(_ss, sep) | ||
} |
Oops, something went wrong.