play

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2023 License: Unlicense Imports: 6 Imported by: 0

README

Play

CI Status GoDoc Go report

A package used for automating the generation of Go Playground shareable URLs. Give it some Go code in the form of []bytes and it returns a URL in one of the following formats:

https://go.dev/play/p/MAohLsrz7JQ
https://play.golang.org/p/MAohLsrz7JQ

Install

go get github.com/wilhelm-murdoch/go-play

Reference

Function NewConfig
  • func NewConfig(base, post, share string) Config #
  • config.go:20:26 #

NewConfig returns a new instance of struct Config using the specified portions of the target Go Playground endpoints.

Function GetPostUrl
  • func (c Config) GetPostUrl() string #
  • config.go:29:31 #

GetPostUrl returns a formatted string representing the full submission URL.

Function GetShareUrl
  • func (c Config) GetShareUrl() string #
  • config.go:34:36 #

GetShareUrl returns a formatted string representing the shareable play URL.

Function NewClient
  • func NewClient(config Config) Client #
  • play.go:22:31 #

NewClient returns a new instance of struct Client using the specified Config instance.

Function FetchGroup
  • func (c Client) FetchGroup(sources [][]byte) (result []string, err error) #
  • play.go:38:60 #

FetchGroup takes a slice of byte slices representing multiple snippets of Go source code to process. This method makes use of the errgroup package which utilises Goroutines to process multiple snippets concurrently. This method stops on the first non-nil error response. The order of resulting slice of strings is not guaranteed.

package main

import (
  "fmt"
  "strings"

  "github.com/wilhelm-murdoch/go-play"
)

func main() {
    var (
    	bytes  [][]byte
    	files  = []string{"play_test.go", "config_test.go"}
    	client = play.NewClient(play.ConfigDefault)
    )
    
    for _, file := range files {
    	code, err := os.ReadFile(file)
    	if err != nil {
    		log.Fatal(err)
    	}
    	bytes = append(bytes, code)
    }
    
    shares, err := client.FetchGroup(bytes)
    if err != nil {
    	log.Fatal(err)
    }
    
    pattern := regexp.MustCompile(playUrlPattern)
    
    for _, share := range shares {
    	fmt.Println(pattern.FindStringIndex(share) != nil)
    }
}
// Output:
// true
// true
Function Fetch
  • func (c Client) Fetch(source []byte) (result string, err error) #
  • play.go:65:78 #

Fetch takes a single slice of bytes representing a snippet of Go source code to process in the Go Playground. This method returns either an error or a shareable Go Playground URL.

package main

import (
  "fmt"
  "strings"

  "github.com/wilhelm-murdoch/go-play"
)

func main() {
    code := []byte(`package main
    
    import "fmt"
    
    func main() {
    fmt.Println("Hello world!")
    }`)
    
    client := play.NewClient(play.ConfigDefault)
    share, err := client.Fetch(code)
    if err != nil {
    	log.Fatal(err)
    }
    
    pattern := regexp.MustCompile(playUrlPattern)
    
    fmt.Println(pattern.FindStringIndex(share) != nil)
}
// Output:
// true
package main

import (
  "fmt"
  "strings"

  "github.com/wilhelm-murdoch/go-play"
)

func main() {
    client := play.NewClient(play.ConfigDefault)
    
    code, err := os.ReadFile("play.go")
    if err != nil {
    	log.Fatal(err)
    }
    
    share, err := client.Fetch(code)
    if err != nil {
    	log.Fatal(err)
    }
    
    pattern := regexp.MustCompile(playUrlPattern)
    
    fmt.Println(pattern.FindStringIndex(share) != nil)
}
// Output:
// true

Documentation generated by Gadget.

License

Copyright © 2022 Wilhelm Murdoch.

This project is MIT licensed.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ConfigDefault = NewConfig("https://go.dev", "_/share", "play/p") // A configuration representing default values for the current Go Playground.
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents an HTTP and Config instance combo used to make calls to the Go Playground.

func NewClient

func NewClient(config Config) Client

NewClient returns a new instance of struct Client using the specified Config instance.

func (Client) Fetch

func (c Client) Fetch(source []byte) (result string, err error)

Fetch takes a single slice of bytes representing a snippet of Go source code to process in the Go Playground. This method returns either an error or a shareable Go Playground URL.

Example (File)
client := play.NewClient(play.ConfigDefault)

code, err := os.ReadFile("play.go")
if err != nil {
	log.Fatal(err)
}

share, err := client.Fetch(code)
if err != nil {
	log.Fatal(err)
}

pattern := regexp.MustCompile(playUrlPattern)

fmt.Println(pattern.FindStringIndex(share) != nil)
Output:

true
Example (String)
code := []byte(`package main

import "fmt"

func main() {
	fmt.Println("Hello world!")
}`)

client := play.NewClient(play.ConfigDefault)
share, err := client.Fetch(code)
if err != nil {
	log.Fatal(err)
}

pattern := regexp.MustCompile(playUrlPattern)

fmt.Println(pattern.FindStringIndex(share) != nil)
Output:

true

func (Client) FetchGroup

func (c Client) FetchGroup(sources [][]byte) (result []string, err error)

FetchGroup takes a slice of byte slices representing multiple snippets of Go source code to process. This method makes use of the `errgroup` package which utilises Goroutines to process multiple snippets concurrently. This method stops on the first non-nil error response. The order of resulting slice of strings is not guaranteed.

Example (File)
var (
	bytes  [][]byte
	files  = []string{"play_test.go", "config_test.go"}
	client = play.NewClient(play.ConfigDefault)
)

for _, file := range files {
	code, err := os.ReadFile(file)
	if err != nil {
		log.Fatal(err)
	}
	bytes = append(bytes, code)
}

shares, err := client.FetchGroup(bytes)
if err != nil {
	log.Fatal(err)
}

pattern := regexp.MustCompile(playUrlPattern)

for _, share := range shares {
	fmt.Println(pattern.FindStringIndex(share) != nil)
}
Output:

true
true

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config exposes URL patterns used to interact with various versions of the Go Playground.

func NewConfig

func NewConfig(base, post, share string) Config

NewConfig returns a new instance of struct Config using the specified portions of the target Go Playground endpoints.

func (Config) GetPostUrl

func (c Config) GetPostUrl() string

GetPostUrl returns a formatted string representing the full submission URL.

func (Config) GetShareUrl

func (c Config) GetShareUrl() string

GetShareUrl returns a formatted string representing the shareable play URL.

Jump to

Keyboard shortcuts

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