Skip to content

Instantly share code, notes, and snippets.

@R4wm
Last active August 11, 2019 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save R4wm/3d41a5b9bac63e8263043da02d5ec629 to your computer and use it in GitHub Desktop.
Save R4wm/3d41a5b9bac63e8263043da02d5ec629 to your computer and use it in GitHub Desktop.
blocking channel demonstration
package main
import "fmt"
const mock = "haha you waited"
func main() {
c := make(chan string)
go func() {
fmt.Println("go routing was run")
// Leveraging closure here with c
c <- mock
}()
fmt.Printf("Ublocking the channel now from main: %s\n", <-c)
}
@R4wm
Copy link
Author

R4wm commented Jul 10, 2019

Blocking demonstration

@arch-lt ~> cat /tmp/blocking.go 
package main

import "fmt"

const mock = "haha you waited"

func main() {

	//////////////////////////////////////////////////////
	// the gofunc will block on the channel		    //
	// to ensure it is run before main continues	    //
	// this is synchronous				    //
	//////////////////////////////////////////////////////
	c := make(chan string)

	go func() {
		fmt.Println("go routing was run")
		// Leveraging closure here with c
		c <- mock
	}()

	fmt.Printf("Ublocking the channel now from main: %s\n", <-c)

}
@arch-lt ~> 
@arch-lt ~> 
@arch-lt ~> go run /tmp/blocking.go
go routing was run
Ublocking the channel now from main: haha you waited
@arch-lt ~> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment