Dedupe Buffered Channel Events

Feed events through a buffered channel and drop repeated keys with a seen map, so burst traffic cannot duplicate downstream work.

GoResilience

Code

gosnippet
23 lines
package main

import "fmt"

func main() {
	// 缓冲通道吸收突发,发送方不阻塞
	events := make(chan string, 8)
	for _, e := range []string{"login", "login", "deploy", "deploy", "push"} {
		events <- e
	}
	close(events)

	seen := make(map[string]bool)
	for event := range events {
		if seen[event] {
			fmt.Println("skip:", event)
			continue
		}
		seen[event] = true
		fmt.Println("process:", event)
	}
	fmt.Println("unique events:", len(seen))
}

Line-by-line reading

Dedupe Buffered Channel Events comes down to the function main.

A single loop drives the file — for _, e := range []string{"login", "login", "deploy", "deploy", "push"}.

What to watch for

  • No external packages — everything relies on the language’s built-in APIs.

Using it

Signature: func main()

Running the file prints labeled results such as skip:, process:, unique events: — each operation’s effect is visible in the output.

Key statements

for _, e := range []string{"login", "login", "deploy", "deploy", "push"} {
events <- e
}
for event := range events {
if seen[event] {
fmt.Println("skip:", event)
continue
}
seen[event] = true
fmt.Println("process:", event)
}
events := make(chan string, 8)

Interactive Tools for This Topic

FAQ

What does Dedupe Buffered Channel Events print when run?
Run it as a complete program — the output lines carry labels such as `skip:`, `process:`, `unique events:`.