โ˜€๏ธ
Dev7Days
  • ๐Ÿ˜„Welcome
  • Local Setup
    • โš™๏ธSetup Terminal
    • โš™๏ธSetup IDE
    • โš™๏ธSetup Neovim
  • Rust
    • ๐Ÿฆ€Cargo
  • Java
    • ๐ŸƒSpring Boot
      • Spring Boot Annotaion
      • Spring Boot Learning
    • ๐ŸƒJDK vs JRE vs JVM
    • ๐ŸƒWhat is JDBC ?
    • ๐ŸƒWhat is Data Source in Java ?
    • ๐ŸƒCheck vs Unchecked Exception
    • ๐ŸƒWhat is Servlet in Java ?
    • ๐ŸƒFilter vs Interceptor
    • ๐ŸƒMockito
    • ๐ŸƒMaven CLI
    • ๐ŸƒMaven Archetype
  • Go
    • ๐Ÿ”นGo Routine and Channel
    • ๐Ÿ”นGo CLI
  • Ruby and Rails
    • โ™ฆ๏ธRuby Syntax
    • โ™ฆ๏ธRails Framework
    • โ™ฆ๏ธRails Structure
  • Fundamental
    • ๐Ÿ“šGit Command
    • ๐Ÿ“šInterpreter vs Compiler
    • ๐Ÿ“šDTO vs DAO
    • ๐Ÿ“šHttp Status
    • ๐Ÿ“šWhat is Batch Process ?
    • ๐Ÿ“šHttps
    • ๐Ÿ“šLocal Storage vs Session Storage vs Cookies
    • ๐Ÿ“šAuthentication & Authorization
    • ๐Ÿ“šDatabase Index
    • ๐Ÿ“šWhat is GRPC ?
    • ๐Ÿ“šWhat is Microservice ?
  • Database
    • ๐Ÿ—ƒ๏ธWhat is Transaction ?
    • ๐Ÿ—ƒ๏ธACID
  • Postgres
    • ๐Ÿ˜SELECT
    • ๐Ÿ˜Column Alias
    • ๐Ÿ˜Order By
    • ๐Ÿ˜SELECT DISTINCT
  • Elastic Search
    • ๐Ÿ”What is Elastic Search ?
    • ๐Ÿ”Node and Cluster
  • Kubernetes
    • โ˜ธ๏ธWhat is Kubernetes ?
    • โ˜ธ๏ธKubernetes Architecture
      • Node
      • ETCD
      • Kube API Server
      • Controller Manager
      • Kube Scheduler
      • Kubelet
      • Kube Proxy
  • โ˜ธ๏ธPod
  • โ˜ธ๏ธReplicaSet
  • โ˜ธ๏ธDeployment
  • โ˜ธ๏ธService
  • โ˜ธ๏ธConfig Map
  • โ˜ธ๏ธNamespaces
  • โ˜ธ๏ธKube Apply Command
  • โ˜ธ๏ธScheduling
    • Manual Scheduling
    • Labels and Selectors
    • Taints and Tolerations
    • Node Selector
    • Node Affinity
    • Resource Requirements and Limits
    • DaemonSets
    • Static Pods
    • MultipleSchedulers
  • โ˜ธ๏ธMonitoring
  • AWS
    • ๐Ÿ”ธHow can users access AWS ?
    • ๐Ÿ”ธIAM
    • ๐Ÿ”ธEC2
      • User Data
      • Instance Types
      • Security Group
      • Purchasing Options
      • Placement Groups
      • Elastic Network Interface (ENI)
      • EC2 Hibernate
      • EC2 Storage
    • ๐Ÿ”ธELB & ASG
      • Health Checks
      • Target Group
      • ELB Types
      • Sticky Sessions
      • Cross Zone Load Balancing
      • Load Balancer - SSL and SNI
      • Deregistration Delay
      • ASG
    • ๐Ÿ”ธRDS & Aurora DB
      • RDS
        • Storage Auto Scaling
        • Read Replica
        • Multi AZ
        • RDS Custom
        • Backup
        • RDS Proxy
      • AWS Aurora
        • Read Replica
        • Endpoint and Auto Scaling
        • Aurora Serverless
        • Global Database
        • Machine Learning
        • Backup
        • Database Cloning
      • RDS & Aurora Restore options
      • RDS & Aurora Security
    • ๐Ÿ”ธElastic Cache
    • ๐Ÿ”ธRoute 53
      • Records
      • Hosted Zones
      • Health Check
      • Routing Policies
  • Backend Security
    • ๐ŸŽฉSQL Injection
    • ๐ŸŽฉCross site script (XSS)
    • ๐ŸŽฉCross site request forgery (CSRF)
    • ๐ŸŽฉMan in the Middle (MITM)
    • ๐ŸŽฉInsecure Direct Object Reference (IDOR)
    • ๐ŸŽฉDistributed denial of service (DDOS)
  • Medium
    • ๐Ÿ‘จโ€๐Ÿ’ปGamer to Coder
    • ๐ŸณDocker
      • Docker #1
      • Docker #2
    • ๐Ÿ’ŠDI and IOC
    • โ˜ธ๏ธKubernetes
  • Book
    • ๐Ÿ“šSystem Design Interview - An Insider's Guide (Volume 1
Powered by GitBook
On this page
  1. Go

Go Routine and Channel

Describe about Go routine and Channel

Go Routine

It is the way that we call function with concurrency in Go that ensuring the response got return and synchronized by Channel or WaitGroup .

Channel

It is similar to queue that receive the response from go routine function by sender so It will ensure the data is synchronized before end the process.

There are 2 type of channels

  1. Unbuffered channels

  2. Buffered channels

Unbuffered Channel

When you create an unbuffered channel, it has a capacity of zero. It mean when the sender send the data to channel it will block the operation until the receiver ready to receive it and the receiver will wait until the sender ready to send data as well.

Example

	start := time.Now()

	ch := make(chan string)

	go func() {
		time.Sleep(3 * time.Second)
		ch <- "finish process"
	}()

	response := <-ch

	// response
	fmt.Println(response)

	// time took
	fmt.Println(time.Since(start))
	
// output
finish process
3.0012505s

Buffered Channel

When you create an buffered channel, It has a specified capcity greater than zero.

It can hold a certain numbers of the values in the channel before blocking then the sender is able to send data to buffer until it full. the difference between unbuffered channel and buffered channel , when the data was sent to channel it will block the operation for receiver so the sender is not able to send the data for "b" because deadlock.

// Unbuffered Channel
        ch := make(chan string)
        ch <- "a"
	ch <- "b"

	// response
	response := <-ch
	fmt.Println(response)
	response2 := <-ch
	fmt.Println(response2)
	
//output 
fatal error: all goroutines are asleep - deadlock!	
// Buffered Channel
        ch := make(chan string, 2)
        ch <- "a"
	ch <- "b"

	// response
	response := <-ch
	fmt.Println(response)
	response2 := <-ch
	fmt.Println(response2)
	
//output 
a
b
54.708ยตs

// For this scenario if we add 
// ch <- "c" after line ch <- "b"
// it will error because the buffer is full.

WaitGroup

Wait groups are created using the sync package, and they provide three essential methods: Add(), Done(), and Wait().

  • Add() is used to add the number of goroutines that need to be waited upon.

  • Done() is called by each goroutine when it finishes its work, decrementing the internal counter of the wait group.

  • Wait() is used to block the execution of the goroutine until all the goroutines have called Done().

Example


var wg sync.WaitGroup

func main() {
	wg.Add(2)
	go func() {
		fmt.Println("process1")
		wg.Done()
	}()
	go func() {
		fmt.Println("process2")
		wg.Done()
	}()
	wg.Wait()
}
PreviousMaven ArchetypeNextGo CLI

Last updated 1 year ago

๐Ÿ”น