feat: add utility functions for environment variable handling, file operations, pointer manipulation, random generation, and string processing
This commit is contained in:
28
utils/jsonx/json.go
Normal file
28
utils/jsonx/json.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package jsonx
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func Marshal(v any) ([]byte, error) {
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
func Unmarshal[T any](data []byte) (*T, error) {
|
||||
var v T
|
||||
err := json.Unmarshal(data, &v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
func UnmarshalTo(data []byte, a any) error {
|
||||
return json.Unmarshal(data, a)
|
||||
}
|
||||
|
||||
func MustUnmarshal[T any](data []byte) *T {
|
||||
v, err := Unmarshal[T](data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
Reference in New Issue
Block a user