This is a creation in Article, where the information may have evolved or changed.
Go code to traverse Slice, look for a slice, count.
Type Element Interface{}func main () { A: = []int{1, 2, 3, 4, 1} for _, I: = Range a { fmt. Println (i) } for I: = 0; i < Len (a); i++ { //fmt. Println (i) } fmt. Println (Index0 (A, 3)) FMT. Println (index0 ([]string{"A", "B", "C", "D", "E"}, "E")) sort. Ints (a)//Sort FMT. Println (a)}//func index0 (a Element, I interface{}) int { If b, OK: = A. ([]int); OK { if c, ok1: = i. (int); Ok1 { for Indexc, V: = range B { if v = = c { return Indexc}}} } if b, OK: = A. ([]str ing); OK { If c, Ok1: = I. (string); Ok1 { for indexc, V: = range B { if v = = c { return Indexc } } } } Return-1}
You can see that slice does not have a way to find an element in the go language above. I'm customizing a method
The Python code below is very concise.
A=[1,2,3,4,1]for B in a: print (b) i=0while I <len (a): print (a[i]) I=i+1#print (sorted (a)) way one sort a.sort () Print (a) print (A.index (3)) A.count (1)