-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_list_test.go
99 lines (83 loc) · 2.62 KB
/
example_list_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2021 Emeka Ugwuanyi. All rights reserved.
// Use of this source code is governed by a MIT License
// license that can be found in the LICENSE file.
package golist_test
import (
"fmt"
"github.com/emylincon/golist"
)
func Example() {
list := golist.NewList([]int{1, 2, 3})
// Get an item in the list by index. `i` represents the index. Returns `nil` if index don't exist.
item := list.Get(0)
fmt.Println("Get(0) :", item)
// To return an item's index in the list, use list.index. works in reverse of `list.Get(i)`. `x` represents the item. Returns `-1` if item don't exist.
index := list.Index(2)
fmt.Println("Index(2) :", index)
// Append an item to the end of the list. Items must be of the same type.
list.Append(7)
fmt.Println("Append(7) :", list)
// Extend the list by appending all the items from a slice or array.
list.Extend([]int{4, 5})
fmt.Println("Extend([]int{4, 5) :", list)
// Reverse elements in list.
fmt.Println("Reverse() :", list.Reverse())
// sum elements in list
fmt.Println("Sum() :", list.Sum())
// remove a given element from list
err := list.Remove(7)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println("Remove(7) :", list)
// Subtract the content of two lists.
list1 := golist.NewList([]int{1, 1})
list2 := golist.NewList([]int{2, 2})
list3, err := list1.ListSubtract(list2)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println("ListSubtract :", list3)
// Multiply all elements in list with no. Example
list1 = golist.NewList([]int{1, 1})
var no int = 2
list3, err = list1.ListMultiplyNo(no)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println("ListMultiplyNo :", list3)
// Divide all elements with other list. Example
list1 = golist.NewList([]int{8, 6})
list2 = golist.NewList([]int{2, 2})
list3, err = list1.ListDivide(list2)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Println("ListDivide :", list3)
// convert list to slice of strings
slice, err := list.ConvertToSliceString()
if err != nil {
fmt.Println(err) // handle error
}
fmt.Printf("ConvertToSliceString: %T\n", slice)
list = golist.NewList([]int{1, 2, 3, 4})
other := golist.NewList([]int{3, 4})
diff, err := list.Difference(other)
if err != nil {
fmt.Println(err) // handle error
}
fmt.Printf("Difference: %v\n", diff)
// Output:
// Get(0) : 1
// Index(2) : 1
// Append(7) : [1, 2, 3, 7]
// Extend([]int{4, 5) : [1, 2, 3, 7, 4, 5]
// Reverse() : [5, 4, 7, 3, 2, 1]
// Sum() : 22
// Remove(7) : [1, 2, 3, 4, 5]
// ListSubtract : [-1, -1]
// ListMultiplyNo : [2, 2]
// ListDivide : [4, 3]
// ConvertToSliceString: []string
// Difference: [1, 2]
}