File tree 1 file changed +19
-0
lines changed
1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,17 @@ func printThis[T any](value T) {
7
7
fmt .Println (value )
8
8
}
9
9
10
+ // index returns the index in the slice for the given item
11
+ // we need to use `comparable` in order to be able to compare values
12
+ func index [T comparable ](vs []T , t T ) int {
13
+ for i , v := range vs {
14
+ if v == t {
15
+ return i
16
+ }
17
+ }
18
+ return - 1
19
+ }
20
+
10
21
// getKeys returns the keys of the map
11
22
// The value type doesn’t have any restraints but the key type should always
12
23
// satisfy the comparable constraint.
@@ -30,4 +41,12 @@ func main() {
30
41
31
42
keysInt := getKeys (map [int ]int {1 : 1 , 2 : 2 })
32
43
fmt .Println (keysInt )
44
+
45
+
46
+ arrStr := []string {"one" , "two" , "three" }
47
+ arrInt := []int {1 , 3 , 2 }
48
+ idxStr := index (arrStr , "three" )
49
+ idxInt := index (arrInt , 3 )
50
+ fmt .Println (idxStr )
51
+ fmt .Println (idxInt )
33
52
}
You can’t perform that action at this time.
0 commit comments