filmov
tv
Go (golang) Tutorials - Concurrency Goroutines

Показать описание
#golang #go #concurrency #goroutines
Concurrency - Goroutines
------------------------
Goroutines
- lightweight threads managed by Go runtime
- resides in the same address space
- Main goroutine should wait for while until all the child goroutines returned back
------------
package main
import ("fmt"
"time"
)
func wish(){
fmt.Println("Wish Goroutine")
}
func main(){ //Main Goroutine
go wish() //Fork a new goroutine to execute the function call for wish()
time.Sleep(1 * time.Second) //Will keep the main goroutine in wait state for 1 second
fmt.Println("Inside Main") //Main goroutine completes its execution
}
-----------
package main
import ("fmt"
"time"
)
func display(name string){
//User For loop {
time.Sleep(1*time.Second)
fmt.Println(i)
fmt.Println(name)
}
}
func main(){
go display("First")
display("Second")
fmt.Println("Main")
}
Concurrency - Goroutines
------------------------
Goroutines
- lightweight threads managed by Go runtime
- resides in the same address space
- Main goroutine should wait for while until all the child goroutines returned back
------------
package main
import ("fmt"
"time"
)
func wish(){
fmt.Println("Wish Goroutine")
}
func main(){ //Main Goroutine
go wish() //Fork a new goroutine to execute the function call for wish()
time.Sleep(1 * time.Second) //Will keep the main goroutine in wait state for 1 second
fmt.Println("Inside Main") //Main goroutine completes its execution
}
-----------
package main
import ("fmt"
"time"
)
func display(name string){
//User For loop {
time.Sleep(1*time.Second)
fmt.Println(i)
fmt.Println(name)
}
}
func main(){
go display("First")
display("Second")
fmt.Println("Main")
}