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
go get github.com/wilhelm-murdoch/go-play
NewConfig returns a new instance of struct Config using the specified portions of the target Go Playground endpoints.
GetPostUrl returns a formatted string representing the full submission URL.
GetShareUrl returns a formatted string representing the shareable play URL.
NewClient returns a new instance of struct Client using the specified Config instance.
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
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.
Copyright © 2022 Wilhelm Murdoch.
This project is MIT licensed.