github.com/giovannyortegon/go@v0.0.0-20220115155912-8890063f5bdd/src/Profesional/Concurrencia/Servidores/servidores.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  )
     8  
     9  func revisarServidor(servidor string, canal chan string) {
    10  
    11  	//fmt.Print(servidor)
    12  	_, err := http.Get(servidor)
    13  	if err != nil {
    14  		canal <- servidor + "No esta disponible"
    15  		//fmt.Println(" No esta disponible")
    16  	} else {
    17  		canal <- servidor + "Esta disponible"
    18  		//fmt.Println(" Essta disponible")
    19  	}
    20  }
    21  
    22  func main() {
    23  
    24  	inicio := time.Now()
    25  
    26  	canal := make(chan string)
    27  
    28  	servidores := []string{
    29  		"https://oregoom.com/",
    30  		"https://www.udemy.com/",
    31  		"https://www.youtube.com/",
    32  		"https://www.facebook.com",
    33  		"https://www.google.com",
    34  	}
    35  
    36  	for _, servidor := range servidores {
    37  		go revisarServidor(servidor, canal)
    38  	}
    39  
    40  	for i := 0; i < len(servidores); i++ {
    41  		fmt.Println(<-canal)
    42  	}
    43  
    44  	Fin := time.Since(inicio)
    45  	fmt.Println("tiempo trancurrido:", Fin)
    46  }