examples: derive examples from the examples directory

With the gotip playground we may want to swap out examples with greater
frequency, to demonstrate new features at Go tip. Make this easier by
deriving examples directly from the examples directory via a new
examplesHandler type. This also enables having dynamic content for
hello.txt, which depends on the value of runtime.GoVersion().

This will impact the non-tip playground in a few ways:
- We will now pre-load examples at server start up.
- Examples will be sorted by their title (with the exception of Hello,
  playground, which is always first).
- We will set a CORS header for examples. This was added for consistency
  with other handlers, and seems harmless.

Generalize TestShare to TestServer, and use it to test the examples

Add a single gotip example demonstrating generics.

Updates golang/go#48517

Change-Id: I7ab58eb391829d581f7aeae95c291666be5718b9
Reviewed-on: https://go-review.googlesource.com/c/playground/+/364374
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alexander Rakoczy <alex@golang.org>
15 files changed
tree: 0184d8b1eb3828cf23abaf5fbb2fa25ea104d8cc
  1. cmd/
  2. deploy/
  3. examples/
  4. internal/
  5. sandbox/
  6. static/
  7. .gitignore
  8. app.gotip.yaml
  9. app.yaml
  11. cache.go
  12. client.go
  13. codereview.cfg
  16. Dockerfile
  17. edit.go
  18. edit.html
  19. enable-fake-time.patch
  20. examples.go
  21. fake_fs.lst
  22. fmt.go
  23. fmt_test.go
  24. go.mod
  25. go.sum
  27. logger.go
  28. main.go
  29. Makefile
  30. metrics.go
  32. play.go
  33. play_test.go
  34. README.md
  35. sandbox.go
  36. sandbox_test.go
  37. server.go
  38. server_test.go
  39. share.go
  40. store.go
  41. tests.go
  42. txtar.go
  43. txtar_test.go
  44. vet.go


Go Reference

This subrepository holds the source for the Go playground: https://play.golang.org/


# build the image
docker build -t golang/playground .


docker run --name=play --rm -p 8080:8080 golang/playground &
# run some Go code
cat /path/to/code.go | go run client.go | curl -s --upload-file - localhost:8080/compile

To run the “gotip” version of the playground, set GOTIP=true in your environment (via -e GOTIP=true if using docker run).


Deployment Triggers

Playground releases automatically triggered when new Go repository tags are pushed to GitHub, or when master is pushed on the playground repository.

For details, see deploy/go_trigger.yaml, deploy/playground_trigger.yaml, and deploy/deploy.json.

Changes to the trigger configuration can be made to the YAML files, or in the GCP UI, which should be kept in sync using the push-cloudbuild-trigger and pull-cloudbuild-trigger make targets.

Deploy via Cloud Build

The Cloud Build configuration will always build and deploy with the latest supported release of Go.

gcloud --project=golang-org builds submit --config deploy/deploy.json .

To deploy the “Go tip” version of the playground, which uses the latest development build, use deploy_gotip.json instead:

gcloud --project=golang-org builds submit --config deploy/deploy_gotip.json .

Deploy via gcloud app deploy

Building the playground Docker container takes more than the default 10 minute time limit of cloud build, so increase its timeout first (note, app/cloud_build_timeout is a global configuration value):

gcloud config set app/cloud_build_timeout 1200  # 20 mins

Alternatively, to avoid Cloud Build and build locally:

make docker
docker tag golang/playground:latest gcr.io/golang-org/playground:latest
docker push gcr.io/golang-org/playground:latest
gcloud --project=golang-org --account=you@google.com app deploy app.yaml --image-url=gcr.io/golang-org/playground:latest


gcloud --project=golang-org --account=you@google.com app deploy app.yaml


To submit changes to this repository, see https://golang.org/doc/contribute.html.