fork download
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. type array []int
  6.  
  7. func (arr array) linearSearch(number int) int {
  8. for i := 0; i < len(arr); i++ {
  9. if arr[i] == number {
  10. return i
  11. }
  12. }
  13.  
  14. return -1
  15. }
  16.  
  17. func main() {
  18. arr := array{8, 2, 9, 10, 5, 4, 2, 7, 18, 0}
  19. number := 7
  20.  
  21. index := arr.linearSearch(number)
  22.  
  23. if index == -1 {
  24. fmt.Println("Liczby nie ma w tablicy")
  25. } else {
  26. fmt.Printf("Liczba jest pod indeksem %d\n", index)
  27. }
  28. }
Success #stdin #stdout 0s 5520KB
stdin
Standard input is empty
stdout
Liczba jest pod indeksem 7