Add select statement example
This commit is contained in:
@@ -12,3 +12,4 @@ https://www.youtube.com/watch?v=ID9NZ88JeOE
|
||||
2. (00:56:07) struct
|
||||
|
||||
//avanzado.go
|
||||
1. (01:12:12) select
|
||||
|
||||
44
avanzado.go
Normal file
44
avanzado.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Saluda() {
|
||||
fmt.Println("Hola desde un goroutine")
|
||||
}
|
||||
|
||||
func imprimeNums(n int) {
|
||||
for i := 0; i < n; i++ {
|
||||
fmt.Println(i)
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
//go -> envia a otro hilo, las goroutinas son concurrentes, se ejecuta en paralelo
|
||||
go Saluda()
|
||||
time.Sleep(time.Second)
|
||||
|
||||
fmt.Println("fin del goroutine")
|
||||
|
||||
go imprimeNums(5)
|
||||
go imprimeNums(5)
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
// chan
|
||||
ch := make(chan int)
|
||||
go func() {
|
||||
ch <- 64
|
||||
}()
|
||||
val := <-ch // espera hasta que se reciba un valor en el canal
|
||||
fmt.Println(val)
|
||||
|
||||
ch2 := make(chan string, 2) // canal con bufer de 2 elementos
|
||||
ch2 <- "hola"
|
||||
ch2 <- "mundo"
|
||||
fmt.Println(<-ch2)
|
||||
fmt.Println(<-ch2)
|
||||
}
|
||||
Reference in New Issue
Block a user