Code Monkey home page Code Monkey logo

go-channel-intro's Introduction

Some Facts about Golang Channel

Use Case Communication between goroutines

The amazing goroutines1 needs something for data exchange and this is where Golang Channels come into play. They are more or less behave like FIFO circular queue.

go func (){
	time.sleep(time.Minute)
	print("world")
}

print("hello ")

Writing and Reading with Channels

ch := make(chan int, 1)
ch <- 3   // sends 3 to the channel
v := <-ch // receives 3 from the channel and assigns it to v

Channel Types and there Use Cases:

  • Buffered Channels (Asynchronous Communication): ch := make(chan int, 3)
  • Unbuffered Channels (Synchronous Communication): ch := make(chan string)
  • Signalling Channels (Async Channels with Zero-sized Elements): ch:= make(chan struct{})

Select Keyword

package main

func main() {
	tick := time.Tick(100 * time.Millisecond)
	boom := time.After(500 * time.Millisecond)
	for {
		select {
		case <-tick:
			fmt.Println("tick.")
		case <-boom:
			fmt.Println("BOOM!")
			return
		default:
			fmt.Println("    .")
			time.Sleep(50 * time.Millisecond)
		}
	}
}

Facts:

  • Channels are blocking for reading and writing
  • Can be directed recvCh := make(<-chan int)
  • Do not share memory (data is copied to the channel struct)
  • Have to be closed or are blocking forever
  • Deadlocks possible, but could be detected

Resources

Footnotes

  1. Powerpoint: Java Virtual Threads and goroutines (Campus Day) โ†ฉ

go-channel-intro's People

Contributors

pixeldrama avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.