#golang #go #concurrency #waitgroups #mutex
Console input
--------------------------
inputdemo.go
-----------------------
package main
import (
"bufio"
"os"
"fmt"
)
func main(){
reader := bufio.NewReader(os.Stdin)
scanner := bufio.NewScanner(os.Stdin)
text,_ := reader.ReadString('\n') //Read a line
fmt.Println(text)
c,size,_:=reader.ReadRune() //Read single char
fmt.Println(c)
fmt.Println(size)
scanner.Scan() //Console input
fmt.Println(scanner.Text())
}
Waitgroup
-------------------
waitdemo.go
---------------------
package main
import (
"fmt"
"sync"
)
var count=0
func countHandler(myWaitGroup *sync.WaitGroup){
count++;
myWaitGroup.Done(); //Notify waitgroup of completion
}
func main(){
var myWaitGroup sync.WaitGroup
for i:=1;i<500;i++ {
myWaitGroup.Add(1) //Wait group counter incremented
go countHandler(&myWaitGroup)
}
myWaitGroup.Wait()
fmt.Println("Value :",count)
}
mutexdemo.go
------------------------
package main
import (
"fmt"
"sync"
)
var count=0
func countHandler(myWaitGroup *sync.WaitGroup,mutex *sync.Mutex){
mutex.Lock() //Mutual Exclusive Lock
count++;
mutex.Unlock() //Releases lock
myWaitGroup.Done(); //Notify waitgroup of completion
}
func main(){
var myWaitGroup sync.WaitGroup
var mutex sync.Mutex
for i:=1;i<=500;i++ {
myWaitGroup.Add(1) //Wait group counter incremented
go countHandler(&myWaitGroup,&mutex)
}
myWaitGroup.Wait()
fmt.Println("Value :",count)
}
Watch video Go (golang) Advanced Tutorials - Mutex and Waitgroup, Console Input online, duration hours minute second in high quality that is uploaded to the channel Ambasoft Java 18 February 2021. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 1,233 times and liked it 28 visitors.