Skip to content

Instantly share code, notes, and snippets.

@emretanriverdi
Created January 17, 2021 18:44
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 emretanriverdi/0d070a7778dd7261d1b35f886ba62d6c to your computer and use it in GitHub Desktop.
Save emretanriverdi/0d070a7778dd7261d1b35f886ba62d6c to your computer and use it in GitHub Desktop.
channels.go
func listenAndUpsert() {
c := make(chan User) // create a channel
go listenFromKafka(c) // pass channel to a goroutine
for msg := range c {
upsertToCouchbase(msg, "key")
}
}
func listenFromKafka(c chan User) {
for {
random := rand.Intn(100) // simulating incoming message
time.Sleep(2 * time.Millisecond) // simulating kafka with 2ms
fmt.Println("# ID " + strconv.Itoa(random) +
" received from Kafka")
c <- User{Id: random}
}
}
func upsertToCouchbase(user User, key string) {
fmt.Println("# upserting to Couchbase for " +
strconv.Itoa(user.Id))
time.Sleep(5 * time.Millisecond)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment