Channels in Go

One of the really intesting features of Go are ‘go routines’ and channels.

Go Routines are the primary way to deal with concurrency in Go.  They’re similar to threads or coprocesses but manage themselves a bit more elegantly.

Channels are essentially references (pointers) that can recieve or sent values and help syncronize communication across Go Routines and serve as blocking calls to values.

Combined together they can be very powerful, allowing for an engineer to let parts of the code run over in their own Go Routine happily chugging away until it needs to interact with the rest of the application and then it can do so via channels.

Enough Talk.  Use this example code and it should show a bit better what is going on with go routines and channels.  You can also view this on my GitHub project.

// Small script file that gives an example of channels in multiple
// go routines.
package main

import (

// Broadcast to channel.
func chanBroadcast(ch chan string) {
	for {
		time.Sleep(time.Second * 3)
		ch <- "Broadcast!"

// Listen on a channel for broadcast.
// Demonstrates blocking nature of channels.
func chanListen(ch chan string) {
	for {
		fmt.Println("... listening ...")
		c := <- ch

// Another technique uses range to assign.
// This is a great technique if you don't need to do something
// before recieving C.
func chanListenTwo(ch chan string) {
	for c := range ch {
		fmt.Println("......... HUH? ....")
		fmt.Println(c + " Two!")

// Setup the go routines and run it all.  Pressing any key ends it.
func main() {
	fmt.Println("PRESS ANY KEY TO EXIT")

	ch := make(chan string)

	go chanBroadcast(ch)
	go chanListen(ch)
	go chanListenTwo(ch)

	var input string


Away I Go

Silly Walk PictureWith the recent announcement that the Go Programming Language reached 1.0 status I thought I’d pick up a bit of it to see what it’s all about and have been pretty impressed so far.

I came into programming via Perl->ASP->PHP->Java->Ruby->Python or there abouts so I don’t have the experience with pointers, channels and all that fun stuff that a lot of people have coming into the industry through C and such.  So I thought it would be a great learning experience for me to pickup Go.

I’ll be posting examples here as I play, particularly of things that were confusing for me at first in hopes it’ll encourage other people to pick up the language as well.  As a newcomer Go is going to be an interesting little project for me.

Like most people, conceptual models are very important for me and as I play with Go I’ll be posting code examples I’ll use to give myself better conceptual models in hopes it’s helpful to someone out there.

Having used it for a bit now I do hope to see it take hold in the community but nobody can really predict these things.  It’s open source from the start, seems to provide all the strength of C or Java without the bloat and with less complexity, is more focused in behaviors than objects and starts with cloud and networking firmly in mind.

Eventually I’d like to put together a MUD with Go for fun that but that is probably a way off.

Wish me luck.