GNU/Emacs mode that setup local Go playground for code snippets like play.golang.org or even better :)

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

  • it works under go-mode with all you plugins installed (like autocompletion, linters and so on)
  • the playground files saved in the subdirectory under GOPATH that helps you keep library of useful snippets
  • it could split the code into arbitrary number of files
  • it could import any external packages from your GOPATH
  • uses vendored packages in your snippet dir
  • could be used with modules with the latest Go versions

Install

Firstly 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"). Personally I recommend use goimports for automatically add import clauses to your snippets. Because usually when you test some ideas you want get working code as fast as possible. And goimports helps with it.

Install go-playground from MELPA:

M-x package-install RET go-playground

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

The mirror repository of the project: https://notabug.org/grafov/go-playground. I really not think that this small project need the mirror but I just would like advertise a nice git hosting for FOSS. Try notabug.org for your projects! :)

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-go-command Set a custom path to go command, possibly with environment (example: "CGO_ENABLED=0 /opt/bin/go").
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.

Example screen after creation of a new snippet:

screenshot

Using with go modules

By default the playground runs go-command as it defined in settings of go-mode. You could use special customized compiler path with go-playground-go-command. For using with modules it could be used such way:

  1. Set go-playground-go-command to "GO111MODULE=on".
  2. Set go-playground-init-command to "go mod init".

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.

Owner
Alexander I.Grafov
I am high skilled master of useless code with a specialization on the architecture of perpetuum mobile engines and cloud castles. I code just because I can.
Alexander I.Grafov
Comments
  • Please delete only playground files...

    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...

  • playground-exec doesn't work under windows.

    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.

  • Fix a bug where starting a playground would change current directory

    Fix a bug where starting a playground would change current directory

    When starting a new playground, before executing the playground initialistaion command, the current directory would be changed to the snippet directory. This would have the side-effect of setting the current directory of whatever buffer the user was in previously, which could cause unexpected behaviour with other commands.

    Instead of changing directory, locally bind default-directory so that the initialisation command will be executed from the snippet directory without an side effects.

  • add autoload to minor mode definition

    add autoload to minor mode definition

    It can be useful for open old snippets from previous sessions. For now if I try to open snippet from previous sessions I see message Ignoring unknown mode ‘go-playground-mode’

  • Prompt for git passphrase

    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.

  • fix: go-playground-upload not working in playground snippet buffers

    fix: go-playground-upload not working in playground snippet buffers

    This fixes the condition in the go-playground-upload function, so it runs go-play-buffer within go-playground snippet file buffers only. Previously it would run it in any buffer, except snippet buffers.

    I also removed the cursor movement and insertion of the result from go-play-upload, as default behavior of go-play-buffer and go-play-region is to place the URL on the killring, and can be customized setting the go-play-browse-function variable.

  • Multi-file support

    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?

  • Improve snippet template comment

    Improve snippet template comment

    Just print commands for snippet removing and uploading as elisp commands in braces. So you could move to end of command and press C-x-e for fastly execute the command without key binding. Usability rules! :)

  • Update go-playground.el

    Update go-playground.el

    go-playground-init-command doesn't work for me, because go mod init needs a package namespace base (i.e: github.com/grafov/) and the current package name.

    Added a template variable to go-playground-init-command to achieve this, so for example you could then do :

    (setq go-playground-init-command "go mod init github.com/grafov/%file-sans")

    and %file-sans would expand to the local filename sans extension....

    imported those function from the multi-compile package https://github.com/ffevotte/multi-compile since well it was already there so may as well use them.

  • Add command for reusing the latest snippet

    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.

Interactive Go interpreter and debugger with REPL, Eval, generics and Lisp-like macros

gomacro - interactive Go interpreter and debugger with generics and macros gomacro is an almost complete Go interpreter, implemented in pure Go. It of

Dec 30, 2022
This vitual os application consist of 3 mini applications embedded in it like weather app , text editor and calculator .

Virtual-Operating-System This vitual os application consist of 3 mini applications embedded in it like weather app , text editor and calculator . APPS

Nov 11, 2021
T# Programming Language. Something like Porth, Forth but written in Go. Stack-oriented programming language.

The T# Programming Language WARNING! THIS LANGUAGE IS A WORK IN PROGRESS! ANYTHING CAN CHANGE AT ANY MOMENT WITHOUT ANY NOTICE! Something like Forth a

Jun 29, 2022
CxGo is a tool for translating C source code to Go

C to Go translator CxGo is a tool for translating C source code to Go (aka transpiler, source-to-source compiler). It uses cc for preprocessing and pa

Jan 1, 2023
A multi-pass compiler written in Go comprised of scanner, recursive-descent parser, generation of AST, intermediate representation (ILOC), and code generation (Armv8).

GoLite Project - Go Huskies! This is a project conducted and led in the course MPCS 51300 Compilers at the University of Chicago. In a group of two, w

Jan 10, 2022
Scriptable interpreter written in golang
Scriptable interpreter written in golang

Anko Anko is a scriptable interpreter written in Go. (Picture licensed under CC BY-SA 3.0, photo by Ocdp) Usage Example - Embedded package main impor

Dec 23, 2022
A bytecode-based virtual machine to implement scripting/filtering support in your golang project.

eval-filter Implementation Scripting Facilities Types Built-In Functions Conditionals Loops Functions Case/Switch Use Cases Security Denial of service

Dec 30, 2022
Gentee - script programming language for automation. It uses VM and compiler written in Go (Golang).

Gentee script programming language Gentee is a free open source script programming language. The Gentee programming language is designed to create scr

Dec 15, 2022
A BASIC interpreter written in golang.
A BASIC interpreter written in golang.

05 PRINT "Index" 10 PRINT "GOBASIC!" 20 PRINT "Limitations" Arrays Line Numbers IF Statement DATA / READ Statements Builtin Functions Types 30 PRINT "

Dec 24, 2022
This package is built for Embedding PHP into Golang.

GoEmPHP This package is built for Embedding PHP into Golang. It is easy to use: script = php.New() script.Startup() defer script.Close()

Jul 2, 2022
PHP bindings for the Go programming language (Golang)

PHP bindings for Go This package implements support for executing PHP scripts, exporting Go variables for use in PHP contexts, attaching Go method rec

Dec 27, 2022
v8 javascript engine binding for golang

Go-V8 V8 JavaScript engine bindings for Go. Features Thread safe Thorough and careful testing Boolean, Number, String, Object, Array, Regexp, Function

Nov 21, 2022
A simple virtual machine - compiler & interpreter - written in golang

go.vm Installation Build without Go Modules (Go before 1.11) Build with Go Modules (Go 1.11 or higher) Usage Opcodes Notes The compiler The interprete

Dec 17, 2022
A JavaScript interpreter in Go (golang)

otto -- import "github.com/robertkrimen/otto" Package otto is a JavaScript parser and interpreter written natively in Go. http://godoc.org/github.com/

Jan 2, 2023
Genetic Algorithms library written in Go / golang

Description Genetic Algorithms for Go/Golang Install $ go install git://github.com/thoj/go-galib.git Compiling examples: $ git clone git://github.com

Sep 27, 2022
Monitorable, gracefully restarting, self-upgrading binaries in Go (golang)
Monitorable, gracefully restarting, self-upgrading binaries in Go (golang)

overseer overseer is a package for creating monitorable, gracefully restarting, self-upgrading binaries in Go (golang). The main goal of this project

Jan 6, 2023
This is a Virtual Operating System made by using GOLANG and FYNE.
This is a Virtual Operating System made by using GOLANG and FYNE.

Virtual-Operating-System This is a Virtual Operating System made by using GOLANG and FYNE. Hello! All In this project I have made a virtual Operating

Nov 1, 2021
Virtual Operating System Using Golang
Virtual Operating System Using Golang

Virtual Operating System Virtual Operating System Using Golang And Fyne Installation 1.Install Go 2.Install Gcc 3.Install Fyne Using This Command:- g

Jun 5, 2022
Curry é um crawler escrito em Golang com a finalidade de verificar o valor do câmbio de USD(Dólar) x BRL(Real) em lojas no Paraguay.

?? Curry - Câmbios ?? Valor do câmbio(USDxBRL) em lojas no Paraguay ?? Sobre Curry é um WebCrawler escrito em Golang com finalidade de verificar os va

Nov 15, 2022