lgo

Go (golang) Jupyter Notebook kernel and an interactive REPL

Disclaimer

Since go1.10, this Go kernel has performance issue due to a performance regression in Go tool chain.

Also, this Go kernel can not be built with go1.12 due to another regression in Go tool chain.

Now, the compiler options this kernel relies on are completely broken and I'm not sure when they will fix the regressions. Unfortunately, they don't plan to fix this in go1.13 as of July 8th 2019. If you are interested in using this kernel, please upvote the bugs. For a while, please use other Go kernels if you want to use the later version of Go with Jupyter notebook.

Medium Post

Features
  • Write and execute Go (golang) interactively like Python.
  • Jupyter Notebook integration
  • Full Go (golang) language spec support. 100% gc (go compiler) compatible.
  • Code completion and inspection in Jupyter Notebooks
  • Display images, HTML, JavaScript, SVG, etc...
  • Currently, lgo is only supported on Linux. But you can use lgo on Mac and Windows with virtual machines or Docker.
Jupyter notebook examples

You can view example notebooks of lgo from Example notebooks on Jupyter nbviewer

If you want to execute these notebooks, you can try these notebooks on your browser without installation from

Try lgo from your browser without installation

Thanks to binder (mybinder.org), you can try lgo on your browsers with temporary docker containers on binder. Open your temporary Jupyter Notebook from the button above and enjoy lgo.

Quick Start with Docker
> git clone https://github.com/yunabe/lgo.git
> cd lgo/docker/jupyter
> docker-compose up -d
8888lgo/docker/jupyter/docker-compose.yamldocker-compose up
  1. Get the URL to open the Jupyter Notebook
> docker-compose exec jupyter jupyter notebook list
Currently running servers:
http://0.0.0.0:8888/?token=50dfee7e328bf86e70c234a2f06021e1df63a19641c86676 :: /examples
  1. Open the Jupyter Notebook server with the authentication token above.

Linux/Mac OS

If you are using Linux or Mac OS, you can use start/stop scripts instead. Web browser will open the URL automatically.

Install

Prerequisites

sudo apt-get install libzmq3-devsudo apt-get install pkg-config

Install

go get github.com/yunabe/lgo/cmd/lgo && go get -d github.com/yunabe/lgo/cmd/lgo-internallgo$(go env GOPATH)/binLGOPATHlgo installLGOPATHLGOPATHlgo installLGOPATHlgo install$LGOPATH/install.loglgo installpkg [packages]LGOPATHLGOPATHlgo installpkg$LGOPATH/installpkg.log[packages]python $(go env GOPATH)/src/github.com/yunabe/lgo/bin/install_kernelpythonjupyterpython3pythonjupyterpip3lgolgojupyter labextension install @yunabe/lgo_extension

Usage: Jupyter Notebook

jupyter notebookShift-TabTabFormat Gojupyter lab

Usage: REPL console

You can use lgo from command line with Jupyter Console or build-in REPL mode of lgo

Jupyter Console (Recommended)

jupyter console --kernel lgo

built-in REPL mode

lgo run
Tips

go get and lgo

$LGOPATHlgo installlgo installgo get

Update go version

lgo install --cleango
lgo install$LGOPATHgogo

Display HTML and images

_ctx.Display_ctx.Display

Cancellation

I, ICtrl-C
Ctrl-Ccontext.Context
_ctx_ctx_ctx

Memory Management

In lgo, memory is managed by the garbage collector of Go. Memory not referenced from any variables or goroutines is collected and released automatically.

[1][2][3]
[2]b[1]bb[3]b[2][2][3][1]
[1]
// Assign 32MB ram to b.
b := make([]byte, 1 << 25)
[2]
// This shadows the first b.
b := make([]byte, 1 << 24)
[3]
// This sets nil to the second b.
b = nil

go1.10

go install
Comparisons with similar projects

gore

gore, which was released in Feb 2015, is the most famous REPL implementation for Go as of Dec 2017. gore is a great tool to try out very short code snippets in REPL style.

maingo run
  1. Loads a very large CSV file as an input. It takes 1 min to load.
  2. Analyzes the loaded data. For example, calculates max, min, avg, etc..

gore always runs the first step when you calculate something and you need to wait for 1 min every time. This behavior is not acceptable for real data science works. Also, gore is not good at tyring code with side effects (even fmt.Println) because code snippets with side effects are executed repeatedly and repeatedly. lgo chose a totally different approach to execute Go code interactively and does not have the same shortcoming.

gore is a CLI tool and it does not support Jupyter Notebook.

gophernotes

lgo gophernotes
Backend gc (go compiler) An unofficial interpreter
Full Go Language Specs ✔️
100% gc compatible ✔️
Static typing ✔️ to some extent
Performance Fast Slow
Overhead 500ms 1ms
✔️
Code completion ✔️
Code inspection ✔️
Code formatting ✔️
✔️
Windows, Mac Use Docker or VM Partial
License BSD LGPL
go run

The shortcomings of using an unofficial interpreter are

interfaceinterfacereflect

The advantages of this approach are

  • The overhead of code execution is small because it does not compile and link code.
  • Windows/Mac partial support. lgo works only on Linux and you need to use VMs or Docker to run it on Windows/Mac. gophernotes (gomacro) works on Windows/Mac natively if you do not need third-party packages.

These disadvantage and advantages are not something inevitable in interperters. But they are not easy to solve under the limited development resource.

Also, lgo kernel supports more rich features in Jupyter Notebook as of Dec 2017, including code completion, code inspection and images/HTML/JavaScript output supports.

Troubleshooting

Dead kernel

Symptom

Got an error message like:

Kernel Restarting
The kernel appears to have died. It will restart automatically.

Solutions

os.Exitos.Exitos.Exitlog.Fatal
os.Exitjupyter notebookdocker attachjupyter notebook
2018/03/01 20:30:45 lgo-internal failed: exit status 1
[I 22:34:00.500 NotebookApp] KernelRestarter: restarting kernel (1/5)
kernel abcd1234-5678-efghi-xxxx-777eeffcccbb restarted
lgo-internal failed

multiple roots

Sympton

Got an error message like:

multiple roots $LGOPATH/pkg &
Failed to build a shared library of github.com/yunabe/lgo/sess7b..7d/exec1: exit status 1

Solutions

gogolgo installgolgo install --cleango
golgo install$LGOPATH

old export format no longer supported

Symptom

Got error messages like:

could not import github.com/yunabe/mylib (/home/yunabe/local/gocode/pkg/linux_amd64/github.com/yunabe/mylib.a: import "github.com/yunabe/mylib": old export format no longer supported (recompile library))

Reason and Solution

$GOPATH$GOPATH
cd $GOPATH/src; go install ./...