goplay

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2016 License: MIT Imports: 8 Imported by: 2

README ΒΆ

goplay - The Go Playground (https://play.golang.org/) client

Travis Build Status Appveyor Build status Go Report Card Coverage Status Releases LICENSE GoDoc

gif

Install

as a Library

go get -u github.com/haya14busa/goplay

as a command line tool

go get -u github.com/haya14busa/goplay/cmd/goplay

Command Line Tool

SYNOPSIS: goplay [OPTION]... [FILE] (With no FILE, or when FILE is -, read standard input.

  -h    show help
  -openbrowser
        open browser automatically (default true)
  -run
        compile and run Go program on The Go Playground (default true)
  -share
        share code on The Go Playground (default true)

🐦 Author

haya14busa (https://github.com/haya14busa)

Documentation ΒΆ

Overview ΒΆ

Package goplay provides The Go Playground (https://play.golang.org/) client

Index ΒΆ

Examples ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

View Source
var DefaultClient = &Client{}

DefaultClient is default Go Playground client.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Client ΒΆ

type Client struct {
	// The base URL of The Go Playground. Default is `https://play.golang.org/`.
	BaseURL string

	// The HTTP client to use when sending requests. Defaults to
	// `http.DefaultClient`.
	HTTPClient *http.Client
}

Client represensts The Go Playground client.

func (*Client) Compile ΒΆ

func (c *Client) Compile(code io.Reader) (*Response, error)

Compile compiles code on The Go Playground.

Example ΒΆ
code := `
package main

import (
	"fmt"
	"time"
)

func main() {
	for i := 3; i > 0; i-- {
		fmt.Printf("%d...\n", i)
		time.Sleep(1 * time.Second)
	}
	fmt.Println("GO!")
}
`
// You can use `DefaultClient` instead of creating `Client` if you want
cli := &Client{HTTPClient: mockCompileClient}
resp, err := cli.Compile(strings.NewReader(code))
if err != nil {
	log.Fatal(err)
}
for _, e := range resp.Events {
	// do not emulate delay.
	// time.Sleep(e.Delay)
	fmt.Print(e.Message)
}
Output:

3...
2...
1...
GO!

func (*Client) Run ΒΆ

func (c *Client) Run(code io.Reader, stdout io.Writer, stderr io.Writer) error

Run runs code which compiled in The Go Playground.

Example ΒΆ
code := `
package main

import "fmt"

func main() {
	fmt.Println("Hello, δΈ–η•Œ!")
}
`
// You can use `DefaultClient` instead of creating `Client` if you want
cli := &Client{HTTPClient: mockRunClient}
if err := cli.Run(strings.NewReader(code), os.Stdout, os.Stderr); err != nil {
	log.Fatal(err)
}
Output:

Hello, δΈ–η•Œ!

func (*Client) Share ΒΆ

func (c *Client) Share(code io.Reader) (string, error)

Share creates go playground share link.

Example ΒΆ
const code = `
package main

import "fmt"

func main() {
	fmt.Println("Hello, δΈ–η•Œ!")
}
`
// You can use `DefaultClient` instead of creating `Client` if you want
cli := &Client{HTTPClient: mockShareClient}
url, err := cli.Share(strings.NewReader(code))
if err != nil {
	log.Fatal(err)
}
fmt.Println(url)
Output:

https://play.golang.org/p/OclbDkg7kv

type Event ΒΆ

type Event struct {
	Message string
	Kind    string        // "stdout" or "stderr"
	Delay   time.Duration // time to wait before printing Message

}

Event represensts event of /compile result.

type Response ΒΆ

type Response struct {
	Errors string
	Events []*Event
}

Response represensts response type of /compile.

Directories ΒΆ

Path Synopsis
cmd
Package socket is client of `golang.org/x/tools/playground/socket`.
Package socket is client of `golang.org/x/tools/playground/socket`.

Jump to

Keyboard shortcuts

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