Skip to content

A package for sending code snippets to Go's playground in exchange for some handy shareable links.

License

Notifications You must be signed in to change notification settings

wilhelm-murdoch/go-play

Repository files navigation

Play

CI Status GoDoc Go report Stability: Active

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.

About

A package for sending code snippets to Go's playground in exchange for some handy shareable links.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Languages