repl.it
@talha131/

Incorrect Go: Iterate Over An Array And Remove Elements

Go

https://www.oncrashreboot.com/iterate-over-an-array-and-remove-elements

fork
loading
Files
  • main.go
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import "fmt"

func remove(slice []int, s int) []int {
	return append(slice[:s], slice[s+1:]...)
}

func main() {
	eg := []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}

	for i, el := range eg {
		if el > 99 {
			eg = remove(eg, i)
      fmt.Println(len(eg))
		}
	}

	fmt.Println(eg)
}