Interactive Go Programming With Jupyter
For the past few years, the programming language Go (golang) is growing in popularity. I was a big fan of Python and I primarily used Python in my hobby projects three years ago. Now, I use Go instead of Python because I can be productive with Go from small-scale hobby projects to very large-scale projects in a large company.
In the same period, Python is also growing in popularity as machine learning and data science became important. There are a lot of reasons why Python is preferred in ML. One reason is Python is designed for interactive code writing and evaluation. Another important reason is there is a great tool for interactive programming in Python. Jupyter Notebook .
Although I now use Go in many projects where I would have used Python before, I still need to use Python for machine learning and data analysis. The ability of interactive programming and Jupyter Notebook in Python are still very attractive to me. I looked for Go Jupyter environment which really works and checked existing golang Jupyter kernels. But there was no such kernel in the world. Some projects were terminated. Some of them are somewhat popular but did not fit to actual use because they do not support either type-safety, code cancellation, code completion, inspection or displaying non-text contents.
Thus, I decided to develop a new environment to run Go interactively on Jupyter Notebook from scratch. Today, Iโm introducing the software I built and the new way to write and execute Go interactively like Python.
Thanks to binder (mybinder.org) , you can try the Go+Jupyter environment (lgo) on your browsers with temporary docker containers on binder. Open your temporary Jupyter Notebook from the button above and enjoy interactive Go programming!
Main 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, inspection and code formatting in Jupyter Notebooks
- Display images, HTML, JavaScript, SVG, etcโฆ
- interactive REPL mode from console
- goroutines and channels are fully supported
Installation ๐
There are two ways to install the Go language Jupyter environment into your computer.
- Use a prebuilt Docker image
- Install from source (Linux only)
If you want to try the Go+Jupyter environment quickly on your computer, try the Docker version first. If you use Linux and you want to integrate the Jupyter environment with the Go environment in your computer, please install it from source. The code execution of lgo is slow in go1.10 due to the regression with -buildmode=shared
. Until the bug is fixed in go1.10, please try lgo with go1.9. It works nicely with go1.9 and go1.8.
In Windows and Mac, please use Docker version because lgo does not support Windows and Mac natively. You can edit notebooks in Windows/Mac from lgo running in Docker.
Usage ๐
Execute jupyter notebook
command to start Jupyter Notebook as usual. When you create a new notebook, please select Go (lgo)
from the menu. Once a new notebook is created, you can write and execute Go interactively like Python.
In lgo, you can show documentation of variables, functions and types by moving the cursor to the identifiers and pressing Shift-Tab
. You can complete code in lgo by pressing Tab
. To display non-text data, use _ctx.
Display like this example
.
Use as a REPL environment from console ๐
You can use lgo as a REPL from console. Run jupyter console --kernel lgo
after you install lgo. Of course, you can use code completion in this mode by pressing Tab
or Ctrl-I
.
>> jupyter console --kernel
In [1]: a, b := 3, 4
In [2]: func sum(x, y int) int {
: return x + y
: }
In [3]: import "fmt"
In [4]: fmt.Sprintf("sum(%d, %d) = %d", a, b, sum(a, b))
sum(3, 4) = 7
Comparison with an existing framework ๐
For those people who know other existing golang Jupyter kernel, here is the comparison table with competitors. Read this section in README.md for details.
lgo | gophernotes | |
---|---|---|
Backend | gc (go compiler) | An unofficial interpreter |
Full Go Language Specs | โ๏ธ | |
100% gc compatible | โ๏ธ | |
Type Safety | โ๏ธ | |
Performance | Fast | Slow |
Overhead | 500ms | 1ms |
Cancellation | โ๏ธ | |
Code completion | โ๏ธ | |
Code inspection | โ๏ธ | |
Display HTML and images | โ๏ธ |
We’re done. If you recommend reading this post, share it on social media.