Skip to content

Instantly share code, notes, and snippets.

@zeroFruit
Created June 30, 2018 03:53
Show Gist options
  • Save zeroFruit/91c278bca903e698fff4053a96fd221b to your computer and use it in GitHub Desktop.
Save zeroFruit/91c278bca903e698fff4053a96fd221b to your computer and use it in GitHub Desktop.
basic channel
package main
import (
"sync"
"fmt"
)
var wg = sync.WaitGroup{}
func main() {
// channel could created with make function
// int type is the data type that could flow through the channel
ch := make(chan int)
wg.Add(2)
go func() {
// receiving data from the channel
// pulling the data from the channel
i := <- ch
fmt.Println("received data")
fmt.Println(i)
wg.Done()
}()
go func() {
// sending data through the channel
fmt.Println("sending data")
i := 42
ch <- i;
i = 27 // pass channel data by copying the data
wg.Done()
}()
wg.Wait()
}
package main
import (
"sync"
"fmt"
)
var wg = sync.WaitGroup{}
func main() {
ch := make(chan int)
go func() {
i := <- ch
fmt.Println(i)
wg.Done()
}()
for j := 0; j < 5; j++ {
wg.Add(2)
go func() {
// This line of code will pause the execution
// until there's a space available in the channel
// We are working with unbuffered channel,
// so there's only one message can be in the channel at one time
ch <- 42
wg.Done()
}()
}
wg.Wait()
}
package main
import (
"sync"
"fmt"
)
var wg = sync.WaitGroup{}
func main() {
ch := make(chan int)
wg.Add(2)
go func() {
// 2. received the data
i := <- ch
fmt.Println(i)
// 3. pushing the data
ch <- 27
wg.Done()
}()
go func() {
// 1. pushing the data
ch <- 42
// 4. received the data
fmt.Println(<-ch)
wg.Done()
}()
wg.Wait()
}
package main
import (
"sync"
"fmt"
)
var wg = sync.WaitGroup{}
func main() {
ch := make(chan int)
wg.Add(2)
// Polymorphic behavior
//
// Receiving data out of the channel only
go func(ch <-chan int) {
i := <- ch
fmt.Println(i)
wg.Done()
}(ch)
// Sending data into the channel only
go func(ch chan<- int) {
ch <- 42
wg.Done()
}(ch)
wg.Wait()
}
package main
import (
"sync"
"fmt"
)
var wg = sync.WaitGroup{}
func main() {
// Buffered channel
//
// Create a channel that's got an internal data store
// What buffered channel is designed to do
// sender or receiver operate at different frequency
// than the other side
ch := make(chan int, 50)
wg.Add(2)
// Polymorphic behavior
//
// Receiving data out of the channel only
go func(ch <-chan int) {
// When closed, range loop handle that then exit
for i := range ch {
fmt.Println(i)
}
wg.Done()
}(ch)
// Sending data into the channel only
go func(ch chan<- int) {
ch <- 42
// We can't detect if a channel is closed
// except for by looking for the application panicking
// we can't recover closed channel
// close(ch)
ch <- 27
// Tell the channel that we are done sending data
close(ch)
wg.Done()
}(ch)
wg.Wait()
}
package main
import (
"sync"
"fmt"
)
var wg = sync.WaitGroup{}
func main() {
// Buffered channel
//
// Create a channel that's got an internal data store
// What buffered channel is designed to do
// sender or receiver operate at different frequency
// than the other side
ch := make(chan int, 50)
wg.Add(2)
// Polymorphic behavior
//
// Receiving data out of the channel only
go func(ch <-chan int) {
for {
// We can detect channel close
// and handle that manually
if i, ok := <- ch; ok {
fmt.Println(i)
} else {
break
}
}
wg.Done()
}(ch)
// Sending data into the channel only
go func(ch chan<- int) {
ch <- 42
// We can't detect if a channel is closed
// except for by looking for the application panicking
// we can't recover closed channel
// close(ch)
ch <- 27
// Tell the channel that we are done sending data
close(ch)
wg.Done()
}(ch)
wg.Wait()
}
package main
import (
"fmt"
"time"
)
const (
logInfo = "INFO"
logWarning = "WARN"
logError = "ERROR"
)
type logEntry struct {
time time.Time
severity string
message string
}
var logCh = make(chan logEntry, 50)
// struct with zero fields is unique in that it requires
// zero memory allocations
// So it can't send any data through
// except for the fact that a message was sent or received
// This is called 'signal only channel'
var doneCh = make(chan struct{})
func main() {
go logger()
//defer func() {
// // gracefully shut down
// close(logCh)
//}()
logCh <- logEntry{time.Now(), logInfo, "App is starting"}
logCh <- logEntry{time.Now(), logInfo, "App is shutting down"}
time.Sleep(100 * time.Millisecond)
// Application is shut down
// as soon as the last statement of the main function finishes
// all the go routines forcibly shut down
// but we need strategy for how go routines gracefully shut down
// Send done singal
doneCh <- struct{}{}
}
func logger() {
for {
// The entire statement is going to block
// until a message is received on one of the channels
select {
case entry := <-logCh:
fmt.Printf("%v - [%v]%v\n", entry.time.Format("2006-01-02T15:04:05"), entry.severity, entry.message)
break
case <-doneCh:
break
// for non-blocking select
//default:
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment