Documentation ¶
Index ¶
- func Chunk[T any](list []T, chunkSize int) [][]T
- func ChunkIndex(length int, chunkSize int) <-chan ChunkedIndex
- func Contains[T any](list []T, elem T) bool
- func DeepCopy(srcModel interface{}, dstModel interface{}) error
- func IndexOf[T any](list []T, elem T) int
- func IsBlank(value reflect.Value) bool
- func IsFirstUpper(v string) bool
- func StructToMap(data interface{}) map[string]interface{}
- type ChunkedIndex
- type ExponentialBackoff
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ChunkIndex ¶ added in v0.0.2
func ChunkIndex(length int, chunkSize int) <-chan ChunkedIndex
ChunkedIndex divides index into chunks
func Contains ¶
Contains returns true if an element is present in a slice
Example ¶
package main import ( "fmt" "github.com/glassonion1/xgo" ) func main() { // slice of int32 containsInt32 := xgo.Contains[int32]([]int32{1, 2, 3, 4, 5}, 3) fmt.Println("contains int32:", containsInt32) // slice of int containsInt := xgo.Contains[int]([]int{1, 2, 3, 4, 5}, 2) fmt.Println("contains int:", containsInt) // slice of float64 containsFloat64 := xgo.Contains[float64]([]float64{1.1, 2.2, 3.3, 4.4, 5.5}, 4.4) fmt.Println("contains float64:", containsFloat64) // slice of struct type hero struct { ID string Name string } list := []hero{ { ID: "1", Name: "Luke Skywalker", }, { ID: "2", Name: "Han Solo", }, { ID: "3", Name: "Leia Organa", }, } target := hero{ ID: "2", Name: "Han Solo", } containsStruct := xgo.Contains[hero](list, target) fmt.Println("contains struct:", containsStruct) }
Output: contains int32: true contains int: true contains float64: true contains struct: true
func DeepCopy ¶
func DeepCopy(srcModel interface{}, dstModel interface{}) error
DeepCopy deepcopy a struct to struct.
Example ¶
package main import ( "fmt" "time" "github.com/glassonion1/xgo" "google.golang.org/protobuf/types/known/timestamppb" ) // It is a common, ordinary struct type FromModel struct { ID string `copier:"Id"` Name string CreatedAt time.Time UpdatedAt *time.Time } // It is like a protobuf struct on gRPC type ToModel struct { Id string Name string CreatedAt *timestamppb.Timestamp UpdatedAt *timestamppb.Timestamp } func main() { now := time.Date(2020, 6, 1, 0, 0, 0, 0, time.UTC) from := FromModel{ ID: "xxxx", Name: "R2D2", CreatedAt: now, UpdatedAt: &now, } to := &ToModel{} err := xgo.DeepCopy(from, to) if err != nil { // handles error } fmt.Println("ToModel object:", to) }
Output: ToModel object: &{xxxx R2D2 seconds:1590969600 seconds:1590969600}
func IsFirstUpper ¶
IsFirstUpper determines whether the first letter is upper case
func StructToMap ¶
func StructToMap(data interface{}) map[string]interface{}
StructToMap converts a struct to map
Types ¶
type ChunkedIndex ¶ added in v0.0.2
type ChunkedIndex struct {
From, To int
}
Index is chunked index that contains from and to
type ExponentialBackoff ¶
type ExponentialBackoff struct {
// contains filtered or unexported fields
}
ExponentialBackoff represents retry with exponential backoff
func NewExponentialBackoff ¶
func NewExponentialBackoff() *ExponentialBackoff
NewExponentialBackoff creates a NewExponentialBackoff instance
Click to show internal directories.
Click to hide internal directories.