gitee.com/quant1x/gox@v1.21.2/api/slices_test.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestReverse0(t *testing.T) {
     9  	x := []string{"1", "2", "3"}
    10  	fmt.Printf("%v\n", x)
    11  	count := len(x)
    12  	for i, j := 0, count-1; i < j; i, j = i+1, j-1 {
    13  		x[i], x[j] = x[j], x[i]
    14  	}
    15  	fmt.Printf("%v\n", x)
    16  }
    17  
    18  func TestReverse(t *testing.T) {
    19  	x := []string{"1", "2", "3", "4"}
    20  	fmt.Printf("%v\n", x)
    21  	xk := Reverse(x)
    22  	fmt.Printf("%v\n", xk)
    23  }
    24  
    25  func TestReverse1(t *testing.T) {
    26  	x := [5]string{"1", "2", "3", "4", "6"}
    27  	fmt.Printf("%v\n", x)
    28  	xk := Reverse(x[:])
    29  	fmt.Printf("%v\n", xk)
    30  }
    31  
    32  func Remove1(slice []interface{}, start, end int) []interface{} {
    33  	result := make([]interface{}, len(slice)-(end-start))
    34  	at := copy(result, slice[:start])
    35  	copy(result[at:], slice[end:])
    36  	return result
    37  }
    38  
    39  // --------------------另一种更为简便的写法-----------------------
    40  func Remove2(slice []interface{}, start, end int) []interface{} {
    41  	return append(slice[:start], slice[end:]...)
    42  }
    43  
    44  func TestFilter(t *testing.T) {
    45  	x := []string{"9", "1", "2", "3", "4", "6"}
    46  	fmt.Printf("%v\n", x)
    47  	xk := Filter(x[:], func(s string) bool {
    48  		return s > "3"
    49  	})
    50  	fmt.Printf("%v\n", xk)
    51  }