package main import ( "fmt" "time" ) const ( w = 79 h = 15 living = 'O' ) type universe [h][w]byte func seed(u *universe) { const c = ` ........................X........... ......................X.X........... ............XX......XX............XX ...........X...X....XX............XX XX........X.....X...XX.............. XX........X...X.XX....X.X........... ..........X.....X.......X........... ...........X...X.................... ............XX......................` for i, v := range c[1:] { if v == 'X' { u[i/37][i%37+10] = living } } } func show(u *universe) { fmt.Print("\x0c") for _, r := range *u { for i, b := range r { if b == 0 { r[i] = ' ' } } fmt.Println(string(r[:])) } } func neighbors(u *universe, x, y int) (n int) { for dx := -1; dx < 2; dx++ { for dy := -1; dy < 2; dy++ { xx, yy := x+dx, y+dy if (dx != 0 || dy != 0) && xx >= 0 && xx < w && yy >= 0 && yy < h && u[yy][xx] == living { n++ } } } return } func main() { u, nxGen := new(universe), new(universe) seed(u) for i := 0; i < 300; i++ { show(u) for y := 0; y < h; y++ { for x := 0; x < w; x++ { live := u[y][x] == living switch n := neighbors(u, x, y); { case live && n < 2: nxGen[y][x] = 0 case live && (n == 2 || n == 3): nxGen[y][x] = living case live && n > 3: nxGen[y][x] = 0 case !live && n == 3: nxGen[y][x] = living default: nxGen[y][x] = 0 } } } u, nxGen = nxGen, u time.Sleep(time.Second / 15) } }

About the Playground

The Go Playground is a web service that runs on golang.org's servers. The service receives a Go program, vets, compiles, links, and runs the program inside a sandbox, then returns the output.

If the program contains tests or examples and no main function, the service runs the tests. Benchmarks will likely not be supported since the program runs in a sandboxed environment with limited resources.

There are limitations to the programs that can be run in the playground:

The playground can use most of the standard library, with some exceptions. The only communication a playground program has to the outside world is by writing to standard output and standard error.

In the playground the time begins at 2009-11-10 23:00:00 UTC (determining the significance of this date is an exercise for the reader). This makes it easier to cache programs by giving them deterministic output.

There are also limits on execution time and on CPU and memory usage.

The article "Inside the Go Playground" describes how the playground is implemented. The source code is available at https://go.googlesource.com/playground.

The playground uses the latest stable release of Go.

The current version is go1.14.8.

The playground service is used by more than just the official Go project (Go by Example is one other instance) and we are happy for you to use it on your own site. All we ask is that you contact us first (note this is a public mailing list), use a unique user agent in your requests (so we can identify you), and that your service is of benefit to the Go community.

Any requests for content removal should be directed to security@golang.org. Please include the URL and the reason for the request.