A simple mode for GNU/Emacs to set up a local Go language playground, offering features similar to — and in some ways surpassing — the play.golang.org service, thanks to go-mode
. Treat it as a simple REPL for Go. Unlike play.golang.org
, this mode provides a complete local playground within Emacs, eliminating the need for a browser or even internet connection to experiment with code snippets.
While the web playground at play.golang.org
is convenient, it comes with restrictions, especially when using additional tools like linters and code completers. Emacs excels in providing these tools for Go development, making it more comfortable to work with snippets directly within Emacs. Setting up a local playground does require configuring the Go environment, but this is necessary for Go development regardless.
First, install go-mode
and gotest
— these are mandatory. Set up any additional tools you need for Go development (see the Emacs Wiki or search for "emacs+golang"). A minimal setup includes goimports
, which automatically adds import clauses to your snippets. For a fully functional Go IDE, consider using lsp
and supplementary packages.
Install go-playground
from MELPA:
M-x package-install RET go-playground
Or:
(use-package go-playground)
To share snippets, use go-play-buffer
from go-mode
. Install gist-buffer
from MELPA if you want to publish gists on github.com.
M-x go-playground
to start a new playground buffer filled with a basic template for the main
package (see the screenshot below).Ctl-C
twice (bound to go-playground-exec
), which saves, compiles, and executes the snippet.M-x go-playground-rm
.go-playground
Create a new playground buffer with a basic template for the main
package. go-playground-download
Download a snippet from a URL at play.golang.org. go-playground-exec
C-c C-c Save, compile, and run the snippet's code. go-playground-cmd
C-u C-c C-c Save the code and prompt for a 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 and all files.
The mode sets default bindings only for two commands, leaving space for your customizations.
List of customizable variables Variable name Descriptiongo-playground-ask-filename
Ask for the snippet filename on a new playground creation. By default, it's named snippet.go
. go-playground-basedir
Default directory for storing snippets. Each new snippet is placed in a new directory under this path. Place it under GOPATH for organization. Introduced in release 1.1. go-playground-confirm-deletion
Confirm before deleting a snippet. You can disable this if it's annoying. Introduced in release 1.2. go-playground-compile-command
Customize the compiler command or run additional commands. Default is "go mod tidy; go run ./...". go-playground-init-command
Shell command to run once when a new snippet is created. go-playground-compiler-args
Arguments passed to the compiler. go-playground-pre-rm-hook
Hook to run before a snippet is removed.
Example screen after creating a new snippet:
If you use lsp-mode
, add a hook to clean up the workspace when a snippet is removed, like this:
(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)
Snippets with Multiple Files and Subpackages
See Issue #19 for details.
Under terms of GPL v3. See LICENSE file.
Future plans and accepting contributionsI aim to keep this package simple, leveraging other packages around go-mode
. Bugfixes and small features are welcome.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4