DEV Community

sai teja
sai teja

Posted on • Updated on


Data Streaming in Go Lang

we may need to do streaming for few things while working with large scale systems like calling multiple API's whenever required and store it in cache if it is frequently used data etc... for these kind of things we may not need to create functions and call them whenever it is needed.Especially when you want to deal data asynchronously.In this case we can simply create a stream and read data from stream whenever it is required.
For readers code will be simple & clean 👍.

updated basic stream... please go thru the code below with explanation and let me know if any doubts / suggestions / mistakes in my code .

package main

import (

func main() {
func streamingDataUsingChannelAndGoRoutines() {
    // send signal to stop channel once you are done with streaming the data (optional)(have to handled by stream owner)
    stop := make(chan bool)

    // it will return read only channel for readers
    publisher := stream(stop)



// it will return read only channel so it will not block the execution / it will not wait to write something into channel/avoids dead lock
func stream(stop <-chan bool) <-chan int {

    // unbuffered channel to stream data
    // this will useful when you want stream live data when ever required
    //ch := make(chan int)

    // buffered channel to stream data
    // this will be useful when you want to store some data in advance  in buffer or cache
    // ans use whenever it is required directly from buffer
    ch := make(chan int,2)

    // this go func will run in background and push data consistently to the channel
    go func() {

        // closing the channel once streaming  gets stop request
        defer close(ch)

        for {
            select {
            case <-stop:
            case ch <- rand.Int(): // eg use case:- we do an api call and store the data here


    return ch
// reader
func read(value <-chan int) {
    // iterating/reading the data for 5 times
    for i := 1; i <= 5; i++ {

        // we can read from channel stream any number of times
        fmt.Println(<-value, <-value, i)



Enter fullscreen mode Exit fullscreen mode

please follow medium for interesting things

Sai Teja – Medium

Read writing from Sai Teja on Medium. Software Engineer | Fitness trainer | Random Thinker. Every day, Sai Teja and thousands of other voices read, write, and share important stories on Medium.


Top comments (0)