Created
March 28, 2025 09:01
-
-
Save dacr/5acd1cb4d0a715c93b2dab03e081bb5c to your computer and use it in GitHub Desktop.
go channels directions / published by https://github.com/dacr/code-examples-manager #54f3f203-2caa-46eb-a07b-4fba5375cb62/b00acb30b96371a3dcf02c55b44ecb58bf9ae56a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*?sr/bin/true; exec /usr/bin/env nix-shell -p go --run "go run $0" #*/ | |
// summary : go channels directions | |
// keywords : go, channels, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 54f3f203-2caa-46eb-a07b-4fba5375cb62 | |
// created-on : 2025-03-27T16:32:35+01:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : nix-shell -p go --run "go run $file" | |
package main | |
import "fmt" | |
func ping(pings chan<- string, message string) { | |
pings <- message | |
} | |
func pong(pings <-chan string, pongs chan<- string) { | |
msg := <-pings | |
pongs <- msg | |
} | |
func main() { | |
pings := make(chan string, 1) | |
pongs := make(chan string, 1) | |
ping(pings, "go ping pong") | |
pong(pings, pongs) | |
fmt.Println(<-pongs) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment