Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 7, 2019 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/5a1ee25e1dde6d5b175d2b871ff07f38 to your computer and use it in GitHub Desktop.
Save parzibyte/5a1ee25e1dde6d5b175d2b871ff07f38 to your computer and use it in GitHub Desktop.
/*
Comprobar si elemento existe dentro de un arreglo
en Go
https://parzibyte.me/blog
*/
package main
import "fmt"
func main() {
arreglo := []int{1, 60, 33, 45, 500}
busqueda := 50
existe := existeEnArreglo(arreglo, busqueda)
fmt.Printf("Buscando %d en arreglo. ¿Existe? %t\n", busqueda, existe)
// Otra búsqueda
busqueda = 45
existe = existeEnArreglo(arreglo, busqueda)
fmt.Printf("Buscando %d en arreglo. ¿Existe? %t", busqueda, existe)
}
func existeEnArreglo(arreglo []int, busqueda int) bool {
for _, numero := range arreglo {
if numero == busqueda {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment