@kjk1/

book channels basics

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.7939339651432009","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"cGFja2FnZSBtYWluCgppbXBvcnQgImZtdCIKCmZ1bmMgbWFpbigpIHsKCS8vIDpzaG93IHN0YXJ0CgkvLyBjcmVhdGUgdW5idWZmZXJlZCBjaGFubmVsIG9mIGludCB2YWx1ZXMgd2l0aCBjYXBhY2l0eSBvZiAxCgljaCA6PSBtYWtlKGNoYW4gaW50KQoJLy8gc3RhcnQgYSBuZXcgZ29yb3V0aW5lIHRoYXQgc2VuZHMgdmFsdWUgMyBvdmVyIGEgY2hhbm5lbAoJZ28gZnVuYygpIHsgY2ggPC0gMyB9KCkKCS8vIHJlYWQgdGhlIHZhbHVlIGZyb20gYSBjaGFubmVsCgkvLyBpdCB3YWl0cyB1bnRpbCBnb3JvdXRpbmUgYWJvdmUgc2VuZHMgYSB2YWx1ZQoJbiA6PSA8LWNoCglmbXQuUHJpbnRmKCJuOiAlZFxuIiwgbikKCS8vIDpzaG93IGVuZAp9Cg=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main

import "fmt"

func main() {
	// :show start
	// create unbuffered channel of int values with capacity of 1
	ch := make(chan int)
	// start a new goroutine that sends value 3 over a channel
	go func() { ch <- 3 }()
	// read the value from a channel
	// it waits until goroutine above sends a value
	n := <-ch
	fmt.Printf("n: %d\n", n)
	// :show end
}