Implementatin from this blog post: https://ggicci.me/goplay-embed-go-playground-on-your-website/
This website uses AWS Cloudfront as a CDN. I used a custom origin to reverse proxy to https://go.dev/ to get around CORS issues by hitting the Go Playground directly. Below is my cloudformation for this Cloudfront distribution:
WebsiteCDN:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
CacheBehaviors:
- TargetOriginId: go-dev
PathPattern: /_/*
ForwardedValues:
QueryString: 'true'
AllowedMethods: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'POST', 'PATCH', 'DELETE']
ViewerProtocolPolicy: redirect-to-https
Compress: true
Comment: CDN for S3-backed website
Aliases:
- !Join ['', ['www', '.', !Ref 'DomainName']]
- !Ref 'DomainName'
Enabled: 'true'
HttpVersion: http2
DefaultCacheBehavior:
ForwardedValues:
QueryString: 'true'
TargetOriginId: the-s3-bucket
ViewerProtocolPolicy: redirect-to-https
Compress: true
DefaultRootObject: index.html
CustomErrorResponses:
- ErrorCachingMinTTL: 300
ErrorCode: 403
ResponseCode: 200
ResponsePagePath: /index.html
- ErrorCachingMinTTL: 300
ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /index.html
Origins:
- DomainName: go.dev
Id: go-dev
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: https-only
- DomainName: !Join ['', [!Ref S3BucketForWebsiteContent, '.s3.amazonaws.com']]
Id: the-s3-bucket
S3OriginConfig:
OriginAccessIdentity:
!Join ['', ['origin-access-identity/cloudfront/', !Ref CloudFrontOriginAccessIdentity]]
ViewerCertificate:
AcmCertificateArn: !Ref 'WebCert'
MinimumProtocolVersion: 'TLSv1.1_2016'
SslSupportMethod: 'sni-only'
Here is an example of how t you can sum an array.
package main
import (
"fmt"
"math/rand/v2"
"time"
)
func main() {
// Go Playground can ony use ~1M of memory
//s := make([]int, 1_024*5120000)
s := make([]int, 1_024*512)
for i := range s {
s[i] = rand.IntN(100)
}
t0 := time.Now()
sum := mergeSum(s)
t1 := time.Now()
fmt.Println("sum:", sum, t1.Sub(t0))
}
func loopSum(s []int) int {
sum:=0
for i:=range s {
sum += s[i]
}
return sum
}
func mergeSum(s []int) int {
if len(s) < 1_024 * 86 {
return loopSum(s)
}
midPoint := len(s) / 2
sums := make(chan int, 2)
go func() {
sums <- mergeSum(s[:midPoint])
}()
func() {
sums <- mergeSum(s[midPoint:])
}()
return <-sums + <-sums
}