@kjk1/

gobook channels close

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.39672446145015416","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImZtdCIKCSJ0aW1lIgopCgpmdW5jIG1haW4oKSB7CgkvLyA6c2hvdyBzdGFydAoJY2ggOj0gbWFrZShjaGFuIHN0cmluZykKCglnbyBmdW5jKCkgewoJCWZvciBzIDo9IHJhbmdlIGNoIHsKCQkJZm10LlByaW50ZigicmVjZWl2ZWQgZnJvbSBjaGFubmVsOiAlc1xuIiwgcykKCQl9CgkJZm10LlByaW50KCJyYW5nZSBsb29wIGZpbmlzaGVkIGJlY2F1c2UgY2ggd2FzIGNsb3NlZFxuIikKCX0oKQoKCWNoIDwtICJmb28iCgljbG9zZShjaCkKCS8vIDpzaG93IGVuZAoKCS8vIG9ubHkgdG8gc2ltcGxpZnkgZXhhbXBsZSwgZG9uJ3Qgc2xlZXAgdG8gY29vcmRpbmF0ZQoJLy8gZ29yb3V0aW5lcyBpbiByZWFsIGNvZGUKCXRpbWUuU2xlZXAoMTAwICogdGltZS5NaWxsaXNlY29uZCkKfQ=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
	"fmt"
	"time"
)

func main() {
	// :show start
	ch := make(chan string)

	go func() {
		for s := range ch {
			fmt.Printf("received from channel: %s\n", s)
		}
		fmt.Print("range loop finished because ch was closed\n")
	}()

	ch <- "foo"
	close(ch)
	// :show end

	// only to simplify example, don't sleep to coordinate
	// goroutines in real code
	time.Sleep(100 * time.Millisecond)
}