Git Product home page Git Product logo

go-playground's Introduction

Local Golang playground MELPA Stable MELPA

The simple mode for GNU/Emacs for setting up local Go language playground with features similar (and with help of go-mode even outperform!) service at play.golang.org. You may treat it like the simple REPL for Go. This is not a wrapper for working with play.golang.org from Emacs (it is already did by original go-mode), this is complete alternative for setting local playground inside Emacs without using browser for experiments with code snippets.

Web playground at play.golang.org is a nice idea. It has not require setup and it executes your code in a restricted environment where you can't damage you system. But it brings many restrictions too especially when you want use additional helpers for working with your code: formatters, code completers etc. Emacs offers many such tools for Go development so it is the most comfortable play with snippets inside Emacs instead of rude webeditor. Of course local playground requres you to setup Go environment but if you developing in Go (or you want to do it) you are anyway would need to setup go-mode and helper tools for working with code in a comfortable way.

Features

  • uses go-mode with all your plugins (like lsp, linters and so on)
  • allow to import external packages
  • keeps the library of snippets under a customized root path
  • support multiple source filcces (or subpackages) for a snippet
  • compatible with Go modules

Install

First install go-mode and gotest โ€” these are mandatory. Setup any additional tools you want for Golang (see Emacs Wiki for guides or google for "emacs+golang"). Minimal way is install goimports that automatically add import clauses to your snippets. For a fully functional Go IDE look at lsp and supplementary packages.

Install go-playground from MELPA:

M-x package-install RET go-playground

Or:

(use-package go-playground)

If you want to share snippets use go-play-buffer from go-mode. Install gist-buffer from MELPA if you want publish gists on github.com.

Usage

Quick start

  1. From any mode run M-x go-playground for start a new playground buffer filled with basic template for main package (see the picture below).
  2. Add your code then press Ctl-Return (it bound to go-playground-exec command). It will save, compile and exec the snippet code.
  3. When you played enough with this snippet just run M-x go-playground-rm. It will remove the current snippet with its directory and all files.

Mode not offers default bindings except Ctl-Return. It just lefts the space for you.

For easy running the template declares main package with defined main() function. Each snippet saved to its own directory (named by timestamp by default). Remember go-playground runs the compiler as go run *.go so any sources from the snippet directory will be included.

List of interactive functions

Function name Description
go-playground Create a new playground buffer with basic template for the main package.
go-playground-download Download the snippet from the URL at play.golang.org.
go-playground-exec Save, compile and run the code of the snippet.
go-playground-cmd Save the code then prompts for the command (compile-mode used).
go-playground-upload Upload the buffer to play.golang.org and return the short URL.
go-playground-rm Remove the snippet with its directory with all files.

List of customizable variables

Function name Description
go-playground-ask-filename Ask for the snippet filename on a new playground creation. By default it got name snippet.go.
go-playground-basedir Default directory where snippets dirs will placed. Each new snippet got a new dir under this. It has sense place the default directory under GOPATH. Var introduced in the release 1.1.
go-playground-confirm-deletion Ask for confirmation before snippet deletion. It may be annoying so you can switch confirmations off. Var introduced in the release 1.2.
go-playground-compile-command Run compiler with custom args or run any other additional commands. By default it runs two command in sequence "go mod tidy; go mod run ./..."
go-playground-init-command Setup shell command that will be run once when a new snippet just created.
go-playground-compiler-args That arguments should be passed for the compiler.
go-playground-pre-rm-hook Hook that should be run before a snippet is removed.

Example screen after creation of a new snippet:

screenshot

Using with lsp-mode

If you use lsp-mode, you can add a hook in your init file to cleanup the workspace when a snippet is removed, for example:

	(defun my/go-playground-remove-lsp-workspace () (when-let ((root (lsp-workspace-root))) (lsp-workspace-folders-remove root)))
	(add-hook 'go-playground-pre-rm-hook #'my/go-playground-remove-lsp-workspace)

Snippet with multiple files and subpackages

Explained here #19.

Similar projects

  • Try go-scratch it even simplier than go-playground. But it prevents you from using many of tools because it not keeps code in files.
  • gorepl-mode is REPL for Go, helpful for interactive research of code.

Licence

Under terms of GPL v3. See LICENSE file.

Future plans and accepting contributions

I don't want to make this package much complex. Most of go-playground functions depends on other packages around go-mode. And it is good way. But one thing I want to add is snippet list buffer where you can easily find and edit/delete snippets of your library. Bugfixes and small features accepted as well.

go-playground's People

Contributors

chmouel avatar grafov avatar guseggert avatar indradhanush avatar jimeh avatar syohex avatar vconcat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go-playground's Issues

Please delete only playground files...

Here is what just happened:

I called go-playground-rm when I'm working on an regular file accidentally(it's the last command I just run), and the whole directory containing this file was deleted, without confirmation...

Multi-file support

Go playground has a multi-file support, like https://play.golang.org/p/rq4J90MxTKJ

package main

import (
	"play.ground/foo"
)

func main() {
	foo.Bar()
}

-- go.mod --
module play.ground

-- foo/foo.go --
package foo

import "fmt"

func Bar() {
	fmt.Println("This function lives in an another file!")
}

Is there any way to execute similar snippet in go-playground?

Prompt for git passphrase

This is probably an issue with my environment rather than a bug with go-playground, but I was wondering if you had any insights on this:

-*- mode: compilation; default-directory: "~/.go/src/playground/at-2019-06-13-150529/" -*-
Compilation started at Thu Jun 13 15:15:09

go run *.go
Enter passphrase for key '/home/user/.ssh/github': 

In the snippet, I'm importing a package that's not in the standard library.

If I run the snippet in a shell with go run the snippet compiles and runs correctly. There's no request for the github key.

I'm on go1.12.5 linux/amd64 and GNU Emacs 26.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.8) of 2019-04-12.

By the way -- thanks for making this -- I use it almost every day.

Add command for reusing the latest snippet

  • Store the path to the latest created snippet to the variable
  • Add command go-playground-latest that would open that snippet instead of creating a new one

Maybe also track the paths for the N latest created snippets and offer the way to reuse them.

Broke find file path.

Run go-playground and switch another buffer, and find-file path is go-playground snippet.go, it should be that buffer path.

playground-exec doesn't work under windows.

windows CMD doesn't automatically expand *, and go run doesn't either, so M-x compile go run *.go doesn't work, giving this error

CreateFile *.go: The filename, directory name, or volume label syntax is incorrect.

which is the same as running go run *.go under CMD or Powershell. Though saving the file and then running M-x eshell go run *.go works fine, of course, as a stopgap work around.

I suspect *.go needs to get expanded before being passed to M-x compile, or some other method used to run the playground.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.