Skip to content

Commit 5ac6e03

Browse files
committed
all: go2goplay.golang.org source
This CL contains the altered playground source that is served at go2goplay.golang.org. It compiles the toolchain at the dev.go2go branch of the main Go repository, translates .go2 programs using the go2go tool, and compiles the playground frontend binary using the dev.go2go toolchain (to support code formatting). The header and “About” text is updated to differentiate from the normal playground and explain its differences, code location, etc. Most features present on play.golang.org are missing, and this instance only supports single-file, non-test, main-package programs. This was done for initial simplicity of the implementation. Updates golang/go#39675 Fixes golang/go#39809 Change-Id: Id9461830c4233ab3f938dc6d21da3e5f2d8adbc3 Reviewed-on: https://go-review.googlesource.com/c/playground/+/240541 Reviewed-by: Alexander Rakoczy <[email protected]>
1 parent 0298bc8 commit 5ac6e03

14 files changed

+174
-136
lines changed

Dockerfile

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
# Use of this source code is governed by a BSD-style
33
# license that can be found in the LICENSE file.
44

5-
ARG GO_VERSION=go1.14.1
6-
75
FROM debian:buster AS go-faketime
86
LABEL maintainer="[email protected]"
97

@@ -13,8 +11,6 @@ RUN apt-get update && apt-get install -y ${BUILD_DEPS} --no-install-recommends
1311
ENV GOPATH /go
1412
ENV PATH /usr/local/go/bin:$GOPATH/bin:$PATH
1513
ENV GO_BOOTSTRAP_VERSION go1.14.1
16-
ARG GO_VERSION
17-
ENV GO_VERSION ${GO_VERSION}
1814

1915
# Get a version of Go for building the playground
2016
RUN curl -sSL https://dl.google.com/go/$GO_BOOTSTRAP_VERSION.linux-amd64.tar.gz -o /tmp/go.tar.gz
@@ -30,14 +26,25 @@ ENV GOPROXY=https://proxy.golang.org
3026

3127
# Compile Go at target sandbox version and install standard library with --tags=faketime.
3228
WORKDIR /usr/local
33-
RUN git clone https://go.googlesource.com/go go-faketime && cd go-faketime && git reset --hard $GO_VERSION
29+
RUN git clone https://go.googlesource.com/go go-faketime && cd go-faketime && git checkout dev.go2go
3430
WORKDIR /usr/local/go-faketime/src
3531
RUN ./make.bash
3632
ENV GOROOT /usr/local/go-faketime
3733
RUN ../bin/go install --tags=faketime std
3834

3935
FROM golang:1.14 as build-playground
4036

37+
# Compile Go using dev.go2go branch to get go2go-compatible go/format.
38+
WORKDIR /usr/local
39+
RUN git clone https://go.googlesource.com/go go2go && cd go2go && git checkout dev.go2go
40+
WORKDIR /usr/local/go2go/src
41+
RUN ./make.bash
42+
ENV GOROOT /usr/local/go2go
43+
44+
WORKDIR /
45+
46+
ENV PATH /usr/local/go2go/bin:$PATH
47+
4148
COPY go.mod /go/src/playground/go.mod
4249
COPY go.sum /go/src/playground/go.sum
4350
WORKDIR /go/src/playground
@@ -55,8 +62,6 @@ RUN apt-get update && apt-get install -y git ca-certificates --no-install-recomm
5562

5663
COPY --from=go-faketime /usr/local/go-faketime /usr/local/go-faketime
5764

58-
ARG GO_VERSION
59-
ENV GO_VERSION ${GO_VERSION}
6065
ENV GOPATH /go
6166
ENV PATH /usr/local/go-faketime/bin:$GOPATH/bin:$PATH
6267

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ GCLOUD_ACCESS_TOKEN := $(shell gcloud auth print-access-token)
77
.PHONY: docker test update-cloudbuild-trigger
88

99
docker:
10-
docker build -t golang/playground .
10+
docker build -t golang/playground-go2go .
1111

12-
runlocal:
12+
runlocal: docker
1313
docker network create sandnet || true
1414
docker kill play_dev || true
15-
docker run --name=play_dev --rm --network=sandnet -ti -p 127.0.0.1:8081:8080/tcp golang/playground --backend-url="http://sandbox_dev.sandnet/run"
15+
docker run --name=play_dev --rm --network=sandnet -ti -p 127.0.0.1:8081:8080/tcp golang/playground-go2go --backend-url="http://sandbox_dev.sandnet/run"
1616

1717
test_go:
1818
# Run fast tests first: (and tests whether, say, things compile)
1919
GO111MODULE=on go test -v ./...
2020

2121
test_gvisor: docker
2222
docker kill sandbox_front_test || true
23-
docker run --rm --name=sandbox_front_test --network=sandnet -t golang/playground --runtests
23+
docker run --rm --name=sandbox_front_test --network=sandnet -t golang/playground-go2go --runtests
2424

2525
# Note: test_gvisor is not included in "test" yet, because it requires
2626
# running a separate server first ("make runlocal" in the sandbox

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ Alternatively, to avoid Cloud Build and build locally:
5656

5757
```bash
5858
make docker
59-
docker tag golang/playground:latest gcr.io/golang-org/playground:latest
60-
docker push gcr.io/golang-org/playground:latest
61-
gcloud --project=golang-org [email protected] app deploy app.yaml --image-url=gcr.io/golang-org/playground:latest
59+
docker tag golang/playground-go2go:latest gcr.io/golang-org/playground-go2go:latest
60+
docker push gcr.io/golang-org/playground-go2go:latest
61+
gcloud --project=golang-org [email protected] app deploy app.yaml --image-url=gcr.io/golang-org/playground-go2go:latest
6262
```
6363

6464
Then:

app.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
service: play
1+
service: go2goplay
22
runtime: custom
33
env: flex
44

@@ -13,7 +13,7 @@ automatic_scaling:
1313
min_num_instances: 5
1414

1515
readiness_check:
16-
path: "/_ah/health"
16+
path: '/_ah/health'
1717
check_interval_sec: 10
1818

1919
env_variables:

edit.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
"cloud.google.com/go/datastore"
1515
)
1616

17-
const hostname = "play.golang.org"
17+
// TODO(golang.org/issue/39675): parameterize before merging.
18+
const hostname = "go2goplay.golang.org"
1819

1920
var editTemplate = template.Must(template.ParseFiles("edit.html"))
2021

@@ -95,7 +96,13 @@ import (
9596
"fmt"
9697
)
9798
99+
func Print(type T)(s []T) {
100+
for _, v := range s {
101+
fmt.Print(v)
102+
}
103+
}
104+
98105
func main() {
99-
fmt.Println("Hello, playground")
106+
Print([]string{"Hello, ", "playground\n"})
100107
}
101108
`

edit.html

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!doctype html>
22
<html>
33
<head>
4-
<title>The Go Playground</title>
4+
<title>The go2go Playground</title>
55
<link rel="stylesheet" href="/static/style.css">
66
{{if .Analytics}}
77
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-11222381-7"></script>
@@ -103,7 +103,11 @@
103103
<body itemscope itemtype="http://schema.org/CreativeWork">
104104
<input type="button" value="Run" id="embedRun">
105105
<div id="banner">
106-
<div id="head" itemprop="name">The Go Playground</div>
106+
<div id="head" itemprop="name">
107+
The
108+
<a href="https://blog.golang.org/generics-next-step">go2go</a>
109+
Playground
110+
</div>
107111
<input type="button" value="Run" id="run">
108112
<input type="button" value="Format" id="fmt">
109113
<div id="importsBox">
@@ -122,11 +126,8 @@
122126
{{end}}
123127
<select class="js-playgroundToysEl">
124128
<option value="hello.txt">Hello, playground</option>
125-
<option value="test.txt">Tests</option>
126-
<option value="multi.txt">Multiple files</option>
127-
<option value="image.txt">Display image</option>
128-
<option value="sleep.txt">Sleep</option>
129-
<option value="clear.txt">Clear</option>
129+
<option value="min.txt">Min function</option>
130+
<option value="queue.txt">Basic queue</option>
130131
</select>
131132
<input type="button" value="About" id="aboutButton">
132133
</div>
@@ -139,17 +140,37 @@
139140
<p><b>About the Playground</b></p>
140141

141142
<p>
142-
The Go Playground is a web service that runs on
143+
The go2go Playground is a web service that runs on
143144
<a href="https://golang.org/">golang.org</a>'s servers.
144145
The service receives a Go program, <a href="https://golang.org/cmd/vet/">vets</a>, compiles, links, and
145146
runs the program inside a sandbox, then returns the output.
146147
</p>
147148

148149
<p>
149-
If the program contains <a href="https://golang.org/pkg/testing">tests or examples</a>
150-
and no main function, the service runs the tests.
151-
Benchmarks will likely not be supported since the program runs in a sandboxed
152-
environment with limited resources.
150+
<b>
151+
This experimental instance of the playground supports the syntax described in the
152+
<a href="https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md">Type Parameters design draft</a>,
153+
utilizing the
154+
<a href="https://go.googlesource.com/go/+/refs/heads/dev.go2go/README.go2go.md">go2go tool</a>.
155+
</b>
156+
</p>
157+
158+
<p>
159+
The best way to provide feedback for the language changes will be on the mailing list [email protected].
160+
Mailing lists are imperfect, but they seem like our best option for initial discussion.
161+
When writing about the design draft, please put [generics] at the start of the Subject line and to start different
162+
threads for different specific topics.
163+
</p>
164+
165+
<p>
166+
If you find bugs in the generics type checker, the translation tool, or this playground instance they should be
167+
filed in the standard Go issue tracker at <a href="https://golang.org/issue/new?title=cmd%2Fgo2go%3A">https://golang.org/issue</a>.
168+
Please start the issue title with cmd/go2go:. Note that the issue tracker is not the best place to discuss
169+
changes to the language, because it does not provide threading and it is not well suited to lengthy conversations.
170+
</p>
171+
172+
<p>
173+
We look forward to your feedback.
153174
</p>
154175

155176
<p>
@@ -158,6 +179,11 @@
158179

159180
<ul>
160181

182+
<li>
183+
Not all features of the normal playground are supported.
184+
<a href="https://golang.org/issue/39675">This issue</a> tracks feature parity with play.golang.org.
185+
</li>
186+
161187
<li>
162188
The playground can use most of the standard library, with some exceptions.
163189
The only communication a playground program has to the outside world
@@ -177,25 +203,8 @@
177203
</ul>
178204

179205
<p>
180-
The article "<a href="https://blog.golang.org/playground" target="_blank" rel="noopener">Inside
181-
the Go Playground</a>" describes how the playground is implemented.
182-
The source code is available at <a href="https://go.googlesource.com/playground" target="_blank" rel="noopener">
183-
https://go.googlesource.com/playground</a>.
184-
</p>
185-
186-
<p>
187-
The playground uses the latest stable release of Go.<br>
188-
The current version is <a href="/p/Ztyu2FJaajl">{{.GoVersion}}</a>.
189-
</p>
190-
191-
<p>
192-
The playground service is used by more than just the official Go project
193-
(<a href="https://gobyexample.com/">Go by Example</a> is one other instance)
194-
and we are happy for you to use it on your own site.
195-
All we ask is that you
196-
<a href="mailto:[email protected]">contact us first (note this is a public mailing list)</a>,
197-
use a unique user agent in your requests (so we can identify you),
198-
and that your service is of benefit to the Go community.
206+
The source code is available at <a href="https://go.googlesource.com/playground/+/refs/heads/dev.go2go" target="_blank" rel="noopener">
207+
https://go.googlesource.com/playground/+/refs/heads/dev.go2go</a>.
199208
</p>
200209

201210
<p>

examples/clear.txt

Lines changed: 0 additions & 18 deletions
This file was deleted.

examples/hello.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import (
44
"fmt"
55
)
66

7+
func Print(type T)(s []T) {
8+
for _, v := range s {
9+
fmt.Print(v)
10+
}
11+
}
12+
713
func main() {
8-
fmt.Println("Hello, playground")
14+
Print([]string{"Hello, ", "playground\n"})
915
}

examples/image.txt

Lines changed: 0 additions & 40 deletions
This file was deleted.

examples/min.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type numeric interface {
8+
type int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
9+
}
10+
11+
func min(type T numeric)(a, b T) T {
12+
if a < b {
13+
return a
14+
}
15+
return b
16+
}
17+
18+
func main() {
19+
fmt.Println(min(42, 84))
20+
fmt.Println(min(3.14159, 2.7182))
21+
}

examples/queue.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type queue(type T) []T
8+
9+
func (q *queue(T)) enqueue(v T) {
10+
*q = append(*q, v)
11+
}
12+
13+
func (q *queue(T)) dequeue() (T, bool) {
14+
if len(*q) == 0 {
15+
var zero T
16+
return zero, false
17+
}
18+
r := (*q)[0]
19+
*q = (*q)[1:]
20+
return r, true
21+
}
22+
23+
func main() {
24+
q := new(queue(int))
25+
q.enqueue(5)
26+
q.enqueue(6)
27+
fmt.Println(q)
28+
fmt.Println(q.dequeue())
29+
fmt.Println(q.dequeue())
30+
fmt.Println(q.dequeue())
31+
}

fmt.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ func handleFmt(w http.ResponseWriter, r *http.Request) {
3434
return
3535
}
3636

37-
fixImports := r.FormValue("imports") != ""
37+
// TODO(golang.org/issue/39675): goimports does not currently support the
38+
// dev.go2go branch.
39+
fixImports := r.FormValue("imports") != "" && false
3840
for _, f := range fs.files {
3941
switch {
4042
case path.Ext(f) == ".go":

0 commit comments

Comments
 (0)