Categorygithub.com/lingdor/magicarray
repository
0.0.17
Repository: https://github.com/lingdor/magicarray.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

MagicArray

NO care type, no care struct, easy to sort and aggregation.similar with php array structure,easy to deal for data process. Coding shortly,process automatically. in magicarray, no nil forever.

go get github.com/lingdor/magicarray

Functions

NameDescribe
array.MakeofMake a MagicArray instance from struct,slice,array,map
array.ValueofStructMake a MagicArray instance from struct, performance better than Makeof.
array.ValueOfSliceMake a MagicArray instance from Slice or array, performance better than Makeof
array.ValueofStructsMake a MagicArray instance from Structs, performance better than Makeof
array.MustValueofMake a MagicArray instance like Makeof,If return error than panic.
array.ValueofMapMake a MagicArray instance from map[string]any, performance better than Makeof
array.MakeMake a empty MagicArray instance
array.EqualsReturn If lenth equal and key and value equals
array.MaxGet the maximum numberic value from the array
array.MinGet the minmum numeric value from the array
array.SumCalculate sum the total numeric values from the array
array.Incheck value is in MagicArray
array.ToStringListtranslate the MagicArray to string array
array.ToIntListtranslate the MagicArray to integer array
array.ToAnyListtranslate the MagicArray to any type of array
array.ToMaptranslate the MagicArray to map
array.ColumnPick the Column from the two-dimensional table data
array.LenGet length of the MagicArray
array.GetGet item from the MagicArray
array.KeysGet keys of the MagicArray
array.ValuesGet values of the MagicArray
array.PickPick the keys and values to a new MagicArray for parameter keys order
array.SetColumnTagSet tags of key column
array.WashColumnWash the value of MagicArray column by rules
array.SetTagSet tag key and value to the value of MagicArray
array.WashAllWash the value of MagicArray all values by rules
array.WashTagRuleJsonInitialLowerWash the value tags ,lower the initial letter if no fund the json tag
array.MergeMerge fields from parameters to MagicArray
array.AppendAppend value to Magic
array.SetSet value of MagicArray
array.RemoveRemove item from the MagicArray
array.Implodejoin MagicArray values to a string
array.Explodesplit the string to MagicArray
array.JsonEncodewrite json to IOWriter
array.JsonMarshalgenerate json bytes and return

Recommend

  1. Create instance of MagicArray
arr1 := ValueofStruct(UserInfo{UserId:123,UserName:"name",})
arr2 := ValueofStructs([]UserInfo{
   {UserId:11,UserName:"name1",},
   {UserId:22,UserName:"name2",}
})
arr3:= ValueOfSlice([]string{"123","456,"789""})
arr4 :=ValueofMap(map[string]any{
    "column1":123,
    "column2":true,
})
// Recommend use the above methods to make instance, that will be lesser to transform calculates.
// at last ,you can use the common method:
arr5 := Valuoef(map[string]string{
    "column1":"123",
    "column2":"1",
})

  1. Iterator to loop the array, for example:
    arr := array.ValueofStruct(IteratorInfo{
        Field1: "field1",
        Field2: 2,
        Field3: true,
    })
    iter := arr.Iter()
    for k, v := iter.FirstKV(); k != nil; k, v = iter.NextKV() {
        fmt.Printf("%s=%s\n", k.String(), v.String())
    }

output:

Field1=field1
Field2=2
Field3=true
  1. JsonMarshal function to generate json, omitEmpty and convert to hump naming rule easily.
users, _ := array.Valueof([]map[string]any{
		{
			"id":        1,
			"user_name": "bobby",
			"Age":       nil,
		}
	})
	bs, _ := array.JsonMarshal(
		users,
		array.JosnOptOmitEmpty(true),
		array.JsonOptDefaultNamingUnderscoreToHump())

	fmt.Println(string(bs))

output:

[{"id":1,"userName":"bobby"}]

Hello world


package main

import (
	"encoding/json"
	"fmt"
	"github.com/lingdor/magicarray/array"
	"time"
)

type UserDTO struct {
	Id   int `json:"userid"`
	Name string
}

type ScoreDTO struct {
	Score     int
	ScoreTime time.Time
}

type AreaDto struct {
	CityId int
	City   string
}

func main() {

	user := UserDTO{
		Id:   1,
		Name: "bobby",
	}
	score := ScoreDTO{
		Score:     66,
		ScoreTime: time.Now(),
	}
	area := AreaDto{
		CityId: 10000,
		City:   "beij",
	}

	mix, _ := array.Merge(array.ValueofStruct(user), score, area)
	mix = array.Pick(mix, "Id", "City", "Score")
	if bs, err := json.Marshal(mix); err == nil {
		fmt.Println(string(bs))
	} else {
		panic(err)
	}

}

output:

{"userid":1,"City":"beij","Score":66}

examples

More of examples, visit _examples/ in my repository.

thanks!