Documentation
¶
Overview ¶
Package gosl provides a snippet collection for working with routine operations in your Go programs with a super user-friendly API and the most efficient performance.
Index ¶
- func Concat(s ...string) string
- func ContainsCaseInsensitive(s, substr string) bool
- func ContainsInMap[T any, K comparable](m map[K]T, key K) bool
- func ContainsInSlice[T comparable](s []T, value T) bool
- func Equals[T comparable](value1, value2 T) bool
- func IsDirExist(path string) bool
- func IsFileExist(path string) bool
- func Marshal[T any](model *T) ([]byte, error)
- func ModifyByValue(m map[string]any, foundValue, newValue any) (foundKey bool, results map[string]any)
- func NotEquals[T comparable](value1, value2 T) bool
- func ParseFileToStruct[T any](path string, model *T) (*T, error)
- func ParseFileWithEnvToStruct[T any](path, envPrefix string, model *T) (*T, error)
- func RandomString(size int) (string, error)
- func RenderStyled(str string, template lipgloss.Style) string
- func ToBytes(s string) ([]byte, error)
- func ToString(b []byte) (string, error)
- func Unmarshal[T any](data []byte, model *T) (*T, error)
- type GenericUtility
- func (g *GenericUtility[T, K]) ContainsInMap(m map[K]T, key K) bool
- func (g *GenericUtility[T, K]) ContainsInSlice(s []K, value K) bool
- func (g *GenericUtility[T, K]) Equals(value1, value2 K) bool
- func (g *GenericUtility[T, K]) Marshal(model *T) ([]byte, error)
- func (g *GenericUtility[T, K]) NotEquals(value1, value2 K) bool
- func (g *GenericUtility[T, K]) ParseFileToStruct(path string, model *T) (*T, error)
- func (g *GenericUtility[T, K]) ParseFileWithEnvToStruct(path, envPrefix string, model *T) (*T, error)
- func (g *GenericUtility[T, K]) Unmarshal(data []byte, model *T) (*T, error)
- type Utility
- func (u *Utility) Concat(s ...string) string
- func (u *Utility) ContainsCaseInsensitive(s, substr string) bool
- func (u *Utility) IsDirExist(path string) bool
- func (u *Utility) IsFileExist(path string) bool
- func (u *Utility) ModifyByValue(m map[string]any, foundValue, newValue any) (foundKey bool, results map[string]any)
- func (u *Utility) RandomString(size int) (string, error)
- func (u *Utility) RenderStyled(s string, template lipgloss.Style) string
- func (u *Utility) ToBytes(s string) ([]byte, error)
- func (u *Utility) ToString(b []byte) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Concat ¶
Concat concatenate strings using the built-in copy and "unsafe" package with unsafe.String function.
If s has no elements returns zero-value for a string.
Example:
package main import ( "fmt" "github.com/koddr/gosl" ) func main() { s1 := "this " s2 := "is " s3 := "my string" s := gosl.Concat(s1, s2, s3) fmt.Println(s) }
func ContainsCaseInsensitive ¶
ContainsCaseInsensitive reports if substr is within s string using built-in "strings" package with strings.Contains. Case-insensitive for input values by default.
If s and/or substr have a zero-value, returns false for bool.
Example:
package main import ( "fmt" "github.com/koddr/gosl" ) func main() { s := "this is my string" substr := "my" b := gosl.ContainsCaseInsensitive(s, substr) fmt.Println(b) }
func ContainsInMap ¶
func ContainsInMap[T any, K comparable](m map[K]T, key K) bool
ContainsInMap reports if key T is within map[T]K.
If m has a zero-value, returns false for bool.
Example:
package main import ( "fmt" "github.com/koddr/gosl" ) func main() { m := map[string]int{"one": 1, "two": 2, "three": 3} key := "two" b := gosl.ContainsInMap(m, key) fmt.Println(b) }
func ContainsInSlice ¶
func ContainsInSlice[T comparable](s []T, value T) bool
ContainsInSlice reports if value T is within slice []T.
If s has a zero-value, returns false for bool.
Example:
package main import ( "fmt" "github.com/koddr/gosl" ) func main() { s := []string{"one", "two", "three"} value := "two" b := gosl.ContainsInSlice(s, value) fmt.Println(b) }
func Equals ¶ added in v1.2.0
func Equals[T comparable](value1, value2 T) bool
Equals compares two values of type T, returns true if they are equal.
Example:
package main import ( "fmt" "github.com/koddr/gosl" ) func main() { s1 := "hello" s2 := "hello" b := gosl.Equals(s1, s2) fmt.Println(b) }
func IsDirExist ¶ added in v1.3.0
IsDirExist reports whether a dir exists on the specified path.
If path has a zero-value or is file, returns false for bool.
Example:
package main import ( "fmt" "github.com/koddr/gosl" ) func main() { p := filepath.Clean("~/Downloads/my-folder") b := gosl.IsDirExist(p) fmt.Println(b) }
func IsFileExist ¶ added in v1.3.0
IsFileExist reports whether a file exists on the specified path.
If path has a zero-value or is dir, returns false for bool.
Example:
package main import ( "fmt" "github.com/koddr/gosl" ) func main() { p := filepath.Clean("~/Downloads/file.csv") b := gosl.IsFileExist(p) fmt.Println(b) }
func Marshal ¶
Marshal converts struct *T to JSON data (byte slice) using jsoniter.Marshal with a default configuration. A 100% compatible drop-in replacement of "encoding/json" standard lib.
If err != nil returns zero-value for a byte slice and error.
Example:
package main import ( "fmt" "log" "github.com/koddr/gosl" ) type user struct { ID int `json:"id"` Name string `json:"name"` } func main() { u := &user{ID: 1, Name: "Viktor"} json, err := gosl.Marshal(u) if err != nil { log.Fatal(err) } fmt.Println(string(json)) }
func ModifyByValue ¶ added in v1.5.0
func ModifyByValue(m map[string]any, foundValue, newValue any) (foundKey bool, results map[string]any)
ModifyByValue modify an unknown key in the given map[string]any by it value. Supports nested maps, but only if their type is map[string]any.
Example:
package main import ( "fmt" "github.com/koddr/gosl" ) func main() { m := map[string]any{"order": map[string]any{"total_cost": 100}} foundValue := 100 newValue := 250 isFound, result := gosl.ModifyByValue(m, foundValue, newValue) fmt.Println(isFound, result) }
func NotEquals ¶ added in v1.2.0
func NotEquals[T comparable](value1, value2 T) bool
NotEquals compares two values of type T, returns true if they are not equal.
Example:
package main import ( "fmt" "github.com/koddr/gosl" ) func main() { s1 := "hello" s2 := "world" b := gosl.NotEquals(s1, s2) fmt.Println(b) }
func ParseFileToStruct ¶ added in v1.6.0
ParseFileToStruct parses the given file from path to struct *T using "knadh/koanf" package.
You can use any of the supported file formats (JSON, YAML, TOML, or HCL). The structured file can be placed both locally (by system path) and accessible via HTTP (by URL).
If err != nil, returns zero-value for a struct and error.
Example:
package main import ( "fmt" "log" "github.com/koddr/gosl" ) type server struct { Host string `koanf:"host"` Port string `koanf:"port"` } func main() { pathToFile := "path/to/server.json" structToParse := &server{} srv, err := gosl.ParseFileToStruct(pathToFile, structToParse) if err != nil { log.Fatal(err) } fmt.Println(srv) }
func ParseFileWithEnvToStruct ¶ added in v1.4.0
ParseFileWithEnvToStruct parses the given file from path to struct *T using "knadh/koanf" package with an (optional) environment variables for a secret data.
You can use any of the supported file formats (JSON, YAML, TOML, or HCL). The structured file can be placed both locally (by system path) and accessible via HTTP (by URL).
If err != nil, returns zero-value for a struct and error.
Example:
package main import ( "fmt" "log" "github.com/koddr/gosl" ) type config struct { ServerURL string `koanf:"server_url"` AuthType string `koanf:"auth_type"` Token string `koanf:"token"` } func main() { pathToFile := "https://github.com/user/repo/config.yaml" envPrefix := "MY_CONFIG" // for ex., MY_CONFIG_TOKEN=my-secret-1234567 structToParse := &config{} cfg, err := gosl.ParseFileWithEnvToStruct(pathToFile, envPrefix, structToParse) if err != nil { log.Fatal(err) } fmt.Println(cfg) }
func RandomString ¶
RandomString generates a random string with a given size using built-in "crypto/rand" and "encoding/hex" packages.
If err != nil returns zero-value for a string and error.
Example:
package main import ( "fmt" "log" "github.com/koddr/gosl" ) func main() { s, err := gosl.RandomString(8) if err != nil { log.Fatal(err) } fmt.Println(s) }
func RenderStyled ¶ added in v1.1.0
RenderStyled render a styled string with a given lipgloss.Style template using "charmbracelet/lipgloss" package.
If s have an "" (empty) value returns zero-value for a string.
Example:
package main import ( "fmt" "log" "github.com/charmbracelet/lipgloss" "github.com/koddr/gosl" ) func main() { tmpl := lipgloss.NewStyle().Foreground(lipgloss.Color("42")).Margin(1) s := gosl.RenderStyled("This is a styled text", tmpl) fmt.Println(s) }
func ToBytes ¶
ToBytes converts string to byte slice using the built-in "unsafe" package with unsafe.Slice function.
If err != nil returns zero-value for a byte slice and error.
Example:
package main import ( "fmt" "log" "github.com/koddr/gosl" ) func main() { s := "this is my string" b, err := gosl.ToBytes(s) if err != nil { log.Fatal(err) } fmt.Println(string(b)) }
func ToString ¶
ToString converts byte slice to string using the built-in "unsafe" package with unsafe.String function.
If err != nil returns zero-value for a string and error.
Example:
package main import ( "fmt" "log" "github.com/koddr/gosl" ) func main() { b := []byte("this is my string") s, err := gosl.ToString(b) if err != nil { log.Fatal(err) } fmt.Println(s) }
func Unmarshal ¶
Unmarshal converts JSON data (byte slice) to struct *T using jsoniter.Unmarshal with a default configuration. A 100% compatible drop-in replacement of "encoding/json" standard lib.
If err != nil returns zero-value for a struct and error.
Example:
package main import ( "fmt" "log" "github.com/koddr/gosl" ) type user struct { ID int `json:"id"` Name string `json:"name"` } func main() { json := []byte(`{"id":1,"name":"Viktor"}`) model := &user{} u, err := gosl.Unmarshal(json, model) if err != nil { log.Fatal(err) } fmt.Println(u) }
Types ¶
type GenericUtility ¶
type GenericUtility[T any, K comparable] struct{}
GenericUtility represents struct with T any and K comparable types for a generic function.
func (*GenericUtility[T, K]) ContainsInMap ¶
func (g *GenericUtility[T, K]) ContainsInMap(m map[K]T, key K) bool
ContainsInMap reports if key T is within map[T]K.
If m have a zero-value returns false for a bool.
func (*GenericUtility[T, K]) ContainsInSlice ¶
func (g *GenericUtility[T, K]) ContainsInSlice(s []K, value K) bool
ContainsInSlice reports if value T is within slice []T.
If s have a zero-value returns false for a bool.
func (*GenericUtility[T, K]) Equals ¶ added in v1.2.0
func (g *GenericUtility[T, K]) Equals(value1, value2 K) bool
Equals compares two values of type K, returns true if they are equal.
func (*GenericUtility[T, K]) Marshal ¶
func (g *GenericUtility[T, K]) Marshal(model *T) ([]byte, error)
Marshal converts struct *T to JSON data (byte slice) using jsoniter.Marshal with a default configuration. A 100% compatible drop-in replacement of "encoding/json" standard lib.
If err != nil returns zero-value for a byte slice and error.
func (*GenericUtility[T, K]) NotEquals ¶ added in v1.2.0
func (g *GenericUtility[T, K]) NotEquals(value1, value2 K) bool
NotEquals compares two values of type K, returns true if they are not equal.
func (*GenericUtility[T, K]) ParseFileToStruct ¶ added in v1.6.0
func (g *GenericUtility[T, K]) ParseFileToStruct(path string, model *T) (*T, error)
ParseFileToStruct parses the given file from path to struct *T using "knadh/koanf" package.
You can use any of the supported file formats (JSON, YAML, TOML, or HCL). The structured file can be placed both locally (by system path) and accessible via HTTP (by URL).
If err != nil, returns zero-value for a struct and error.
func (*GenericUtility[T, K]) ParseFileWithEnvToStruct ¶ added in v1.4.0
func (g *GenericUtility[T, K]) ParseFileWithEnvToStruct(path, envPrefix string, model *T) (*T, error)
ParseFileWithEnvToStruct parses the given file from path to struct *T using "knadh/koanf" package with an (optional) environment variables for a secret data.
You can use any of the supported file formats (JSON, YAML, TOML, or HCL). The structured file can be placed both locally (by system path) and accessible via HTTP (by URL).
If err != nil, returns zero-value for a struct and error.
func (*GenericUtility[T, K]) Unmarshal ¶
func (g *GenericUtility[T, K]) Unmarshal(data []byte, model *T) (*T, error)
Unmarshal converts JSON data (byte slice) to struct *T using jsoniter.Unmarshal with a default configuration. A 100% compatible drop-in replacement of "encoding/json" standard lib.
If err != nil returns zero-value for a struct and error.
type Utility ¶
type Utility struct{}
Utility represents struct for a regular function.
func (*Utility) Concat ¶
Concat concatenate strings using the built-in copy and "unsafe" package with unsafe.String function.
If s has no elements returns zero-value for a string.
func (*Utility) ContainsCaseInsensitive ¶
ContainsCaseInsensitive reports if substr is within s string using built-in "strings" package with strings.Contains. Case-insensitive for input values by default.
If s and/or substr have an "" (empty) value returns false for a bool.
func (*Utility) IsDirExist ¶ added in v1.3.0
IsDirExist reports whether a dir exists on the specified path.
func (*Utility) IsFileExist ¶ added in v1.3.0
IsFileExist reports whether a file exists on the specified path.
func (*Utility) ModifyByValue ¶ added in v1.5.0
func (u *Utility) ModifyByValue(m map[string]any, foundValue, newValue any) (foundKey bool, results map[string]any)
ModifyByValue modify an unknown key in the given map[string]any by it value. Supports nested maps, but only if their type is map[string]any.
func (*Utility) RandomString ¶
RandomString generates a random string with a given size using built-in "crypto/rand" and "encoding/hex" packages.
If err != nil returns zero-value for a string and error.
func (*Utility) RenderStyled ¶ added in v1.1.0
RenderStyled render a styled string with a given lipgloss.Style template using "charmbracelet/lipgloss" package.
If s have an "" (empty) value returns zero-value for a string.