Add channels

This commit is contained in:
Sangeeth Sudheer 2024-04-27 15:20:18 +05:30
parent 27dda5f2b6
commit 132e8a621f
Signed by: x
GPG Key ID: F6D06ECE734C57D1
2 changed files with 50 additions and 2 deletions

View File

@ -0,0 +1,46 @@
package channels
import (
"fmt"
"strings"
"unicode/utf8"
)
func header(title string) {
fmt.Println(strings.Repeat("-", utf8.RuneCountInString(title)))
fmt.Println(title)
fmt.Println(strings.Repeat("-", utf8.RuneCountInString(title)))
fmt.Println()
}
func simpleChannels() {
header("Simple channels")
message := make(chan string)
go func() {
message <- "beep bop!"
}()
msg := <-message
fmt.Println("Received secret message:", msg)
fmt.Println("Done")
}
func bufferedChannels() {
header("Buffered channels")
message := make(chan string, 2)
message <- "secret 1!"
message <- "secret 2!"
fmt.Println("First message:", <-message)
fmt.Println("Second message:", <-message)
fmt.Println("Done")
}
func Channels() {
simpleChannels()
bufferedChannels()
}

View File

@ -4,12 +4,14 @@ package main
// import "git.sangeeth.dev/gobyexample/structs"
// import "git.sangeeth.dev/gobyexample/generics"
// import "git.sangeeth.dev/gobyexample/errors"
import "git.sangeeth.dev/gobyexample/goroutines"
// import "git.sangeeth.dev/gobyexample/goroutines"
import "git.sangeeth.dev/gobyexample/channels"
func main() {
// runes.Runes()
// structs.Structs()
// generics.Generics()
// errors.Errors()
goroutines.Goroutines()
// goroutines.Goroutines()
channels.Channels()
}