pic: don't make a string copy when writing image

Previously, the entire image was encoded into a buffer,
then encoded to a base64 string, which was concatenated
with another string, and written to stdout in one swoop.

We can do this more efficiently by writing the PNG image
to a base64 encoder directly, and using buffered I/O to
batch writes to stdout into moderately-sized chunks.

As an added bonus, though really the main motivation for
doing this optimization now, this helps avoid triggering
the golang/go#38751 issue on the Go Playground.

Switch to the best compression level instead of the default,
reducing encoded image size at the cost of extra computation.
Playground snippets need to be transferred over the network
and kept in storage, so it should be a favorable trade-off.

This CL was based on CL 232177.

For golang/go#38751.

Change-Id: I565fe538aa15910caaff98be156ac64b0d35fff4
Co-authored-by: Alexander Rakoczy <alex@golang.org>
Reviewed-on: https://go-review.googlesource.com/c/tour/+/232867
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alexander Rakoczy <alex@golang.org>
2 files changed
tree: 9229c438172e51ed7364bd167f538313e74165a3
  1. .gcloudignore
  6. README.md
  7. TODO
  9. app.yaml
  10. appengine.go
  11. codereview.cfg
  12. content/
  13. fmt.go
  14. go.mod
  15. go.sum
  16. gotour/
  17. local.go
  18. pic/
  19. reader/
  20. solutions/
  21. static/
  22. template/
  23. tools/
  24. tour.go
  25. tree/
  26. wc/

Go Tour

A Tour of Go is an introduction to the Go programming language. Visit https://tour.golang.org to start the tour.


To install the tour from source, first install Go and then run:

$ go get golang.org/x/tour

This will place a tour binary in your workspace's bin directory. The tour program can be run offline.


Contributions should follow the same procedure as for the Go project: https://golang.org/doc/contribute.html

To run the tour server locally:

go run .

Your browser should now open. If not, please visit http://localhost:3999/.

Report Issues / Send Patches

This repository uses Gerrit for code changes. To learn how to submit changes to this repository, see https://golang.org/doc/contribute.html.

The issue tracker for the tour's code is located at https://github.com/golang/go/issues. Prefix your issue with “x/tour:” in the subject line, so it is easy to find.

Issues with the tour's content itself should be reported in the issue tracker at https://github.com/golang/tour/issues.


  1. To deploy tour.golang.org, run:

    GO111MODULE=on gcloud --project=golang-org app deploy --no-promote app.yaml

    This will create a new version, which can be viewed within the golang-org GCP project.

  2. Check that the deployed version looks OK (click the version link in GCP).

  3. If all is well, click “Migrate Traffic” to move 100% of the tour.golang.org traffic to the new version.

  4. You're done.


Unless otherwise noted, the go-tour source files are distributed under the BSD-style license found in the LICENSE file.