github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/isc/test/group_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/isyscore/isc-gobase/isc"
     7  )
     8  
     9  type GroupStruct struct {
    10  	Key  string
    11  	Name string
    12  	Age  int
    13  }
    14  
    15  func TestGroupBy(t *testing.T) {
    16  	list := []GroupStruct{
    17  		{Key: "K", Name: "库陈胜", Age: 1},
    18  		{Key: "K", Name: "库陈胜1", Age: 2},
    19  		{Key: "K", Name: "库陈胜2", Age: 3},
    20  		{Key: "K1", Name: "库陈胜", Age: 1},
    21  		{Key: "K1", Name: "库陈胜2", Age: 2},
    22  		{Key: "K2", Name: "库陈胜3", Age: 1},
    23  	}
    24  	m := isc.GroupBy(list, func(t GroupStruct) string {
    25  		return t.Key
    26  	})
    27  	t.Logf("%v", m)
    28  }
    29  
    30  func TestGroupByTransform(t *testing.T) {
    31  	list := []GroupStruct{
    32  		{Key: "K", Name: "库陈胜", Age: 1},
    33  		{Key: "K", Name: "库陈胜1", Age: 2},
    34  		{Key: "K", Name: "库陈胜2", Age: 3},
    35  		{Key: "K1", Name: "库陈胜", Age: 1},
    36  		{Key: "K1", Name: "库陈胜2", Age: 2},
    37  		{Key: "K2", Name: "库陈胜3", Age: 1},
    38  	}
    39  	m := isc.GroupByTransform(list, func(t GroupStruct) string {
    40  		return t.Key
    41  	}, func(t GroupStruct) int {
    42  		return t.Age
    43  	})
    44  	t.Logf("%v", m)
    45  }