goplayground

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2022 License: MIT Imports: 12 Imported by: 2

README

Go Playground Client

This is a client of The Go Playground.

Use as CLI

Install
$ go install github.com/tenntenn/goplayground/cmd/gp@latest
Usage
$ gp help
Run
$ gp run main.go
$ gp run a.go b.go
$ find . -type f | xargs gp run
$ find . -type f -not -path '*/\.*' | xargs gp run
Format
$ gp format [-imports] main.go
$ gp format [-imports] -output main.go main.go
$ gp format a.go b.go
$ find . -type f | xargs gp format
$ find . -type f -not -path '*/\.*' | xargs gp format
Share
$ gp share main.go
$ gp share a.go b.go
$ find . -type f | xargs gp share
$ find . -type f -not -path '*/\.*' | xargs gp share
Download
$ gp download https://go.dev/play/p/sTkdodLtokQ
$ gp dl https://play.golang.org/p/sTkdodLtokQ
$ gp dl -dldir=output https://go.dev/play/p/sTkdodLtokQ

Version

version prints Go version of playground.

$ gp version
Version: go1.17.5
Release: go1.17
Name: Go 1.17
$ gp version -backend gotip
Version: devel go1.18-2c58bb2e42 Wed Jan 5 09:50:29 2022 +0000
Release: go1.18
Name: Go dev branch

With Go dev branch

$ gp format -backend gotip example.go
$ gp run -backend gotip example.go
$ gp share -backend gotip example.go
$ gp download -backend gotip hYtdQPeKUC3

Use as a libary

See: https://pkg.go.dev/github.com/tenntenn/goplayground

Documentation

Index

Constants

View Source
const (
	// FrontBaseURL is frontend of the Go Playground.
	FrontBaseURL = "https://go.dev/play"
	// BaseURL is the default base URL of the Go Playground.
	BaseURL = "https://play.golang.org"
	// Deprecated: Go2GoBaseURL is the base URL of go2goplay.golang.org.
	Go2GoBaseURL = "https://go2goplay.golang.org"
	// Version is version of using Go Playground.
	Version = "2"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend added in v0.3.0

type Backend string

Backend indicates run Go environment.

const (
	// BackendDefault indicates default environment (latest release version).
	BackendDefault Backend = ""
	// BackendGotip indicates using the develop branch.
	BackendGotip Backend = "gotip"
)

func (*Backend) Set added in v0.3.0

func (b *Backend) Set(s string) error

Set implements flag.Value.Set.

func (Backend) String added in v0.3.0

func (b Backend) String() string

String implements flag.Value.String.

type Client

type Client struct {
	FrontBaseURL string
	BaseURL      string
	Backend      Backend
	HTTPClient   HTTPClient
}

Client is a client of Go Playground. If BaseURL is empty, Client uses default BaseURL. HTTPClient can be set instead of http.DefaultClient.

func (*Client) Download

func (cli *Client) Download(w io.Writer, hashOrURL string) error

Download downloads source code hosted on Playground. The source would be written into w.

func (*Client) Format

func (cli *Client) Format(src interface{}, imports bool) (*FormatResult, error)

Format formats the given src by gofmt or goimports. src can be set string, []byte and io.Reader value. If imports is true, Format formats and imports unimport packages with goimports.

func (*Client) Run

func (cli *Client) Run(src interface{}) (*RunResult, error)

Run compiles and runs the given src. src can be set string, []byte and io.Reader value.

func (*Client) Share

func (cli *Client) Share(src interface{}) (*url.URL, error)

Share generates share URL of the given src. src can be set string, []byte and io.Reader value.

func (*Client) Version added in v0.3.0

func (cli *Client) Version() (*VersionResult, error)

Version gets version and release tags which is used in the Go Playground.

type FormatResult

type FormatResult struct {
	// Body is the formatted source code.
	Body string
	// Error is a gofmt error.
	Error string
}

FormatResult is result of Client.Format.

type HTTPClient

type HTTPClient interface {
	// Do method send a HTTP request.
	Do(*http.Request) (*http.Response, error)
}

HTTPClient is an interface of minimum HTTP client. net/http.Client implements this interface.

type HTTPClientFunc

type HTTPClientFunc func(*http.Request) (*http.Response, error)

HTTPClientFunc implements HTTPClient.

func (HTTPClientFunc) Do

func (f HTTPClientFunc) Do(req *http.Request) (*http.Response, error)

Do implements HTTPClient.Do.

type RunEvent

type RunEvent struct {
	// Message is a message which is outputed to stdout or stderr.
	Message string
	// Kind has stdout or stderr value.
	Kind string
	// Delay represents delay time to print the message to stdout or stderr.
	Delay time.Duration
}

RunEvent represents output events to stdout or stderr of Client.Run.

type RunResult

type RunResult struct {
	// Errors is compile or runtime error on Go Playground.
	Errors string
	// Events has output events on Go Playground.
	Events []*RunEvent
}

RunResult is result of Client.Run.

type VersionResult added in v0.3.0

type VersionResult struct {
	Version string
	Release string
	Name    string
}

VersionResult is result of Client.Format.

Directories

Path Synopsis
cmd
gp command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL