pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/sliceutil/example_test.go (about)

     1  package sliceutil
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"fmt"
    12  )
    13  
    14  // ////////////////////////////////////////////////////////////////////////////////// //
    15  
    16  func ExampleCopy() {
    17  	s1 := []string{"A", "B", "C"}
    18  	s2 := Copy(s1)
    19  
    20  	fmt.Printf("%v", s2)
    21  	// Output: [A B C]
    22  }
    23  
    24  func ExampleCopyInts() {
    25  	s1 := []int{1, 2, 3}
    26  	s2 := CopyInts(s1)
    27  
    28  	fmt.Printf("%v", s2)
    29  	// Output: [1 2 3]
    30  }
    31  
    32  func ExampleCopyFloats() {
    33  	s1 := []float64{1.0, 5.0}
    34  	s2 := CopyFloats(s1)
    35  
    36  	fmt.Printf("%v", s2)
    37  	// Output: [1 5]
    38  }
    39  
    40  func ExampleStringToInterface() {
    41  	s := []string{"A", "B"}
    42  
    43  	fmt.Printf("%v", StringToInterface(s))
    44  	// Output: [A B]
    45  }
    46  
    47  func ExampleIntToInterface() {
    48  	s := []int{1, 2}
    49  
    50  	fmt.Printf("%v", IntToInterface(s))
    51  	// Output: [1 2]
    52  }
    53  
    54  func ExampleErrorToString() {
    55  	s := []error{fmt.Errorf("error1")}
    56  
    57  	fmt.Printf("%v", ErrorToString(s))
    58  	// Output: [error1]
    59  }
    60  
    61  func ExampleIndex() {
    62  	s := []string{"A", "B", "C"}
    63  
    64  	fmt.Println(Index(s, "C"))
    65  	fmt.Println(Index(s, "D"))
    66  	// Output: 2
    67  	// -1
    68  }
    69  
    70  func ExampleExclude() {
    71  	s := []string{"A", "B", "C", "D"}
    72  
    73  	fmt.Println(Exclude(s, "B", "D"))
    74  	// Output: [A C]
    75  }
    76  
    77  func ExampleDeduplicate() {
    78  	s := []string{"A", "A", "A", "B", "C", "C", "D"}
    79  
    80  	fmt.Println(Deduplicate(s))
    81  	// Output: [A B C D]
    82  }