You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

1 Días
21 Hrs
1 Min
23 Seg

Ciclos While y For

27/42
Resources

Contributions 3

Questions 1

Sort by:

Want to see more contributions, questions and answers from the community?

Cabe mencionar que realmente no necesitamos el if dentro del ciclo, puesto que GO cuenta con el “for while” que es basicamente subir el if en la misma declaracion del for de la siguiente manera:

for i < 2 {
		
	for _, servidor := range servidores {
		go revisarServidor(servidor, canal)
	}
	time.Sleep(1 * time.Second)
	fmt.Println(<-canal)
	i++
}

Y esto tendra exactamente el mismo resultado y puede ser incluso un poco mas prolijo

Aquí pueden ver mas ejemplos de for en go

Buenas, lo modifique un poquito
Esta es la secuencia
Envía los 4 GO
Espera los 4 canales
Duerme 1 segundo
Y el lazo infinito, tiene un contador de 10

package main

import(
	"fmt"
	"net/http"
	"time"
)

func revisarServidor(servidor string , canal chan string)  {
	_ , err := http.Get(servidor)
	if err != nil{
		canal <- servidor + " No Funciona"
	}else{
		canal <- servidor + " Si Funciona"
	}

}

func main()  {

	inicio := time.Now()

	canal := make(chan string)

	servidores := []string {
		"https://platzi.com",
		"https://google.com",
		"https://facebook.com",
		"https://instagram.com",
	}

	infinito := 0

	for {
		for _,servidor := range servidores{
			go revisarServidor(servidor, canal)		
		}
	
		
		for i := 0 ; i < len(servidores) ; i++{
			fmt.Println("Indice: " , infinito , <- canal)	
		}

		time.Sleep( 1 * time.Second) 

		infinito++

		if infinito == 10{
			break
		}

	}



	tiempoPaso := time.Since(inicio)
	fmt.Printf("Tiempo de espera %s \n", tiempoPaso)
}