Thursday, 24 September 2020

golang4 range

 package main

import (
"fmt"
)

func main() {
var a []int = []int{5, 6, 7, 8, 5, 4, 3, 2}

for i, element := range a {
fmt.Printf("index: %d, value: %d\n", i, element)
}

//find duplicate
for i, element := range a {
for j := i + 1; j < len(a); j++ {
element2 := a[j]
if element2 == element {
fmt.Printf("\nduplicate found: %d\n", element)
}
}
}
}

//cmd
C:\Users\bob\golang1>go run tutorial.go
index: 0, value: 5
index: 1, value: 6
index: 2, value: 7
index: 3, value: 8
index: 4, value: 5
index: 5, value: 4
index: 6, value: 3
index: 7, value: 2

duplicate found: 5

reference:

No comments:

Post a Comment