{"id":13425064,"url":"https://github.com/yunabe/lgo","last_synced_at":"2025-05-15T15:04:17.444Z","repository":{"id":41374490,"uuid":"105907218","full_name":"yunabe/lgo","owner":"yunabe","description":"Interactive Go programming with Jupyter","archived":false,"fork":false,"pushed_at":"2020-11-20T07:01:33.000Z","size":992,"stargazers_count":2449,"open_issues_count":24,"forks_count":123,"subscribers_count":46,"default_branch":"master","last_synced_at":"2025-04-07T20:09:30.302Z","etag":null,"topics":["data-science","go","golang","jupyter-notebook","jupyter-notebook-kernel","machine-learning","repl"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yunabe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-05T15:29:10.000Z","updated_at":"2025-03-31T04:40:45.000Z","dependencies_parsed_at":"2022-07-19T01:33:01.438Z","dependency_job_id":null,"html_url":"https://github.com/yunabe/lgo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunabe%2Flgo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunabe%2Flgo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunabe%2Flgo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunabe%2Flgo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yunabe","download_url":"https://codeload.github.com/yunabe/lgo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254364270,"owners_count":22058878,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-science","go","golang","jupyter-notebook","jupyter-notebook-kernel","machine-learning","repl"],"created_at":"2024-07-31T00:01:03.716Z","updated_at":"2025-05-15T15:04:17.376Z","avatar_url":"https://github.com/yunabe.png","language":"Go","readme":"# lgo [![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/yunabe/lgo-binder/master?filepath=basics.ipynb) [![Go Report Card](https://goreportcard.com/badge/github.com/yunabe/lgo)](https://goreportcard.com/report/github.com/yunabe/lgo)\n\nGo (golang) Jupyter Notebook kernel and an interactive REPL\n\n## Disclaimer\n\nSince go1.10, this Go kernel has performance issue due to [a performance regression in Go tool chain](https://github.com/golang/go/issues/24034).\n\nAlso, this Go kernel can not be built with go1.12 due to [another regression in Go tool chain](https://github.com/golang/go/issues/30768).\n\nNow, the compiler options this kernel relies on are completely broken and I'm not sure when they will fix the regressions.\nUnfortunately, 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.\n\n## Medium Post\n- [Go (golang) Jupyter Notebook kernel and an interactive REPL - April 16, 2018](https://medium.com/@yunabe/interactive-go-programming-with-jupyter-93fbf089aff1)\n\n# Features\n- Write and execute Go (golang) interactively like Python.\n- Jupyter Notebook integration\n- Full Go (golang) language spec support. 100% gc (go compiler) compatible.\n- Code completion and inspection in Jupyter Notebooks\n- Display images, HTML, JavaScript, SVG, etc...\n- Currently, lgo is only supported on Linux. But you can use lgo on Mac and Windows with virtual machines or Docker.\n\n\u003cimg src=\"https://drive.google.com/uc?export=view\u0026id=12_7fHfKfdSy8SNXi0nsWznbsRgix9tGJ\" width=\"400\" height=\"366\"\u003e\n\n# Jupyter notebook examples\nYou can view example notebooks of lgo from\n[Example notebooks on Jupyter nbviewer](https://nbviewer.jupyter.org/github/yunabe/lgo/blob/master/examples/basics.ipynb)\n\nIf you want to execute these notebooks, you can try these notebooks on your browser without installation from [![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/yunabe/lgo-binder/master?filepath=basics.ipynb)\n\n# Try lgo from your browser without installation\n[![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/yunabe/lgo-binder/master?filepath=basics.ipynb)\n\nThanks to [binder (mybinder.org)](https://mybinder.org/), you can try lgo on your browsers with temporary docker containers on binder.\nOpen your temporary Jupyter Notebook from the button above and enjoy lgo.\n\n# Quick Start with Docker\n1. Install [Docker](https://docs.docker.com/engine/installation/) and [Docker Compose](https://docs.docker.com/compose/).\n2. Clone the respository and run the docker container with docker-compose.\n```\n\u003e git clone https://github.com/yunabe/lgo.git\n\u003e cd lgo/docker/jupyter\n\u003e docker-compose up -d\n```\n\nIf you want to use a port other than `8888` on host, update ports config in `lgo/docker/jupyter/docker-compose.yaml` before running `docker-compose up`.\n\n3. Get the URL to open the Jupyter Notebook\n```\n\u003e docker-compose exec jupyter jupyter notebook list\nCurrently running servers:\nhttp://0.0.0.0:8888/?token=50dfee7e328bf86e70c234a2f06021e1df63a19641c86676 :: /examples\n```\n4. Open the Jupyter Notebook server with the authentication token above.\n\n## Linux/Mac OS\nIf you are using Linux or Mac OS, you can use start/stop scripts instead. Web browser will open the URL automatically.\n\n```bash\n# start server\n\u003e ./up.sh\n# stop server\n\u003e ./down.sh\n```\n\n# Install\n## Prerequisites\n- lgo is supported only on Linux at this moment. On Windows or Mac OS, use virtual machines or dockers.\n- [Install Go 1.9 or Go 1.8](https://golang.org/doc/install).\n  - [Note for Go 1.10](#go110)\n- Install [Jupyter Notebook](http://jupyter.readthedocs.io/en/latest/install.html)\n- [Install ZMQ](http://zeromq.org/distro:debian)\n  - e.g. `sudo apt-get install libzmq3-dev`\n- [Install pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/)\n  - e.g. `sudo apt-get install pkg-config`\n\n## Install\n- `go get github.com/yunabe/lgo/cmd/lgo \u0026\u0026 go get -d github.com/yunabe/lgo/cmd/lgo-internal`\n  - This installs `lgo` command into your `$(go env GOPATH)/bin`\n- Set `LGOPATH` environment variable\n  - `lgo install` will install binaries into the directory specified with `LGOPATH`.\n  - You can use any empty directory with write permission as `LGOPATH`.\n- Run `lgo install`\n  - This installs std libraries and the internal lgo tool into `LGOPATH` with specific compiler flags.\n  - If `lgo install` fails, please check install log stored in `$LGOPATH/install.log`\n- (Optional) Run `lgo installpkg [packages]` to install third-party packages to `LGOPATH`\n  - You can preinstall third-party packages into `LGOPATH`.\n  - This step is optional. If packages are not preinstalled, lgo installs the packages on the fly.\n  - But, installing packages is a heavy and slow process. I recommend you to preinstall packages\n    which you will use in the future with high probability.\n  - If `lgo installpkg` fails, please check the log stored in `$LGOPATH/installpkg.log`.\n  - See [go's manual](https://golang.org/cmd/go/#hdr-Package_lists) about the format of `[packages]` args.\n- Install the kernel configuration to Jupyter Notebook\n  - `python $(go env GOPATH)/src/github.com/yunabe/lgo/bin/install_kernel`\n  - Make sure to use the same version of `python` as you used to install `jupyter`. For example, use `python3` instead of `python` if you install `jupyter` with `pip3`.\n- (Optional) If you want to use `lgo` with JupyterLab, install a jupyterlab extension for `lgo`\n  - `jupyter labextension install @yunabe/lgo_extension`\n  - This extension adds \"Go Format\" button to the toolbar in JupyterLab.\n\n## Usage: Jupyter Notebook\n- Run `jupyter notebook` command to start Juyputer Notebook and select \"Go (lgo)\" from New Notebook menu.\n- To show documents of packages, functions and variables in your code, move the cursor to the identifier you want to inspect and press `Shift-Tab`.\n- Press `Tab` to complete code\n- Click `Format Go` button in the toolbar to format code.\n- lgo works with [JupyterLab](https://github.com/jupyterlab/jupyterlab). To use lgo from JupyterLab, install JupyterLab and run `jupyter lab`.\n\n\u003cimg width=\"400\" height=\"225\" src=\"doc/inspect.jpg\"\u003e\n\u003cimg width=\"400\" height=\"225\" src=\"doc/complete.jpg\"\u003e\n\n## Usage: REPL console\nYou can use lgo from command line with [Jupyter Console](https://github.com/jupyter/jupyter_console) or build-in REPL mode of lgo\n\n### Jupyter Console (Recommended)\nRun `jupyter console --kernel lgo`\n\n```go\nIn [1]: a, b := 3, 4\n\nIn [2]: func sum(x, y int) int {\n      :     return x + y\n      :     }\n\nIn [3]: import \"fmt\"\n\nIn [4]: fmt.Sprintf(\"sum(%d, %d) = %d\", a, b, sum(a, b))\nsum(3, 4) = 7\n```\n\n### built-in REPL mode\nRun `lgo run`\n\n```go\n$ lgo run\n\u003e\u003e\u003e a, b := 3, 4\n\u003e\u003e\u003e func sum(x, y int) int {\n...     return x + y\n...     }\n\u003e\u003e\u003e import \"fmt\"\n\u003e\u003e\u003e fmt.Sprintf(\"sum(%d, %d) = %d\", a, b, sum(a, b))\nsum(3, 4) = 7\n```\n\n# Tips\n## go get and lgo\nThe packages you want to use in lgo must be prebuilt and installed into `$LGOPATH` by `lgo install` command.\nPlease make sure to run `lgo install` after you fetch a new package with `go get` command.\n\n## Update go version\nPlease run `lgo install --clean` after you update `go` version.\n\n`lgo install` installs prebuilt packages into `$LGOPATH`.\nWhen you update `go` version, you need to reinstall these prebuilt packages with the newer `go`\nbecause binary formats of prebuilt packages may change in the newer version of go.\n\n## Display HTML and images\nTo display HTML and images in lgo, use [`_ctx.Display`](https://godoc.org/github.com/yunabe/lgo/core#LgoContext).\nSee [the example of `_ctx.Display`](http://nbviewer.jupyter.org/github/yunabe/lgo/blob/master/examples/basics.ipynb#Display) in an example notebook\n\n## Cancellation\nIn lgo, you can interrupt execution by pressing \"Stop\" button (or pressing `I, I`) in Jupyter Notebook and pressing `Ctrl-C` in the interactive shell.\n\nHowever, as you may know, Go does not allow you to cancel running goroutines with `Ctrl-C`. Go does not provide any API to cancel specific goroutines. The standard way to handle cancellation in Go today is to use [`context.Context`](https://golang.org/pkg/context/#Context) (Read [Go Concurrency Patterns: Context](https://blog.golang.org/context) if you are not familiar with context.Context in Go).\n\nlgo creates a special context `_ctx` on every execution and `_ctx` is cancelled when the execution is cancelled. Please pass `_ctx` as a context.Context param of Go libraries you want to cancel. Here is [an example notebook of cancellation in lgo](http://nbviewer.jupyter.org/github/yunabe/lgo/blob/master/examples/interrupt.ipynb).\n\n## Memory Management\nIn lgo, memory is managed by the garbage collector of Go. Memory not referenced from any variables or goroutines is collected and released automatically.\n\nOne caveat of memory management in lgo is that memory referenced from global variables are not released automatically when the global variables are shadowed by other global variables with the same names. For example, if you run the following code blocks, the 32MB RAM reserved in `[1]` is not released after executing `[2]` and `[3]` because\n\n- `[2]` does not reset the value of `b` in `[1]`. It just defines another global variable `b` with the same name and shadows the reference to the first `b`.\n- `[3]` resets `b` defined in `[2]`. The memory reserved in `[2]` will be released after `[3]`. But the memory reserved in `[1]` will not be released.\n\n```\n[1]\n// Assign 32MB ram to b.\nb := make([]byte, 1 \u003c\u003c 25)\n[2]\n// This shadows the first b.\nb := make([]byte, 1 \u003c\u003c 24)\n[3]\n// This sets nil to the second b.\nb = nil\n```\n\n## go1.10\nlgo works with go1.10. But the overhead of code execution is 4-5x larger in go1.10 than go1.9.\nIt is due to [a regression of the cache mechnism of `go install` in go1.10](https://github.com/golang/go/issues/24034).\nI recommend you to use lgo with go1.9 until the bug is fixed in go1.10.\n\n# Comparisons with similar projects\n## gore\n[gore](https://github.com/motemen/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.\n\nBut gore does not fit to data science or heavy data processing at all.\ngore executes your inputs by concatinating all of your inputs,\nwrapping it with `main` function and running it with `go run` command.\nThis means every time you input your code, gore executes all your inputs from the begining.\nFor example, if you are writing something like\n\n1. Loads a very large CSV file as an input. It takes 1 min to load.\n2. Analyzes the loaded data. For example, calculates max, min, avg, etc..\n\ngore always runs the first step when you calculate something and you need to wait for 1 min every time.\nThis 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.\nlgo chose a totally different approach to execute Go code interactively and does not have the same shortcoming.\n\ngore is a CLI tool and it does not support Jupyter Notebook.\n\n## gophernotes\n||lgo|gophernotes|\n|:---|:---|:---|\n|Backend|gc (go compiler)|An unofficial interpreter|\n|Full Go Language Specs|:heavy_check_mark:||\n|100% gc compatible|:heavy_check_mark:||\n|Static typing|:heavy_check_mark:|to some extent|\n|Performance|Fast|Slow|\n|Overhead|500ms|1ms|\n|[Cancellation](https://github.com/yunabe/lgo/blob/master/README.md#cancellation)|:heavy_check_mark:||\n|Code completion|:heavy_check_mark:||\n|Code inspection|:heavy_check_mark:||\n|Code formatting|:heavy_check_mark:||\n|[Display HTML and images](https://github.com/yunabe/lgo/blob/master/README.md#display-html-and-images)|:heavy_check_mark:||\n|Windows, Mac|Use Docker or VM|Partial|\n|License|BSD|LGPL|\n\ngophernotes was the first Jupyter kernel for Go, released in Jan 2016.\nBefore [Sep 2017](https://github.com/gopherdata/gophernotes/commit/69792d8af799d6905e2c576164d1a189ac021784#diff-04c6e90faac2675aa89e2176d2eec7d8), it used the same technology gore uses to evaluate Go code. This means it did not fit to heavy data processing or data analysis at all.\nFrom Sep 2017, gophernotes switched from `go run` approach to [gomacro](https://github.com/cosmos72/gomacro), one of unofficial golang interpreters by [cosmos72](https://github.com/cosmos72). This solved the problem gore has. Now, the code execution mechnism of gophernotes also fits to heavy data analysis.\n\nThe shortcomings of using an unofficial interpreter are\n- It does not support all Go language features. Especially, it does not support one of the most important Go feature, `interface`.\n  As of go1.10, it is hard to support `interface` in an interpreter written in Go because of the lack of API in `reflect` package.\n- Interpreters are generally slow.\n- Unofficial interpreters are not well-tested compared to the official gc (go compiler) tools.\n\nThe advantages of this approach are\n- The overhead of code execution is small because it does not compile and link code.\n- Windows/Mac partial support. lgo works only on Linux and you need to use VMs or Docker to run it on Windows/Mac.\n  gophernotes (gomacro) works on Windows/Mac natively [if you do not need third-party packages](https://github.com/gopherdata/gophernotes#limitations).\n\nThese disadvantage and advantages are not something inevitable in interperters. But they are not easy to solve under the limited development resource.\n\nAlso, lgo kernel supports more rich features in Jupyter Notebook as of Dec 2017, including code completion, code inspection and images/HTML/JavaScript output supports.\n\n# Troubleshooting\n\n## Dead kernel\n### Symptom\nGot an error message like:\n\n```\nKernel Restarting\nThe kernel appears to have died. It will restart automatically.\n```\n\n### Solutions\nFirst, please confirm your code does not call [`os.Exit`](https://golang.org/pkg/os/#Exit) directly or indirectly.\nIn lgo, your code is executed in the processs of lgo kernel. If you evaluate `os.Exit` in lgo, it terminates the lgo kernel process and jupyter notebook server loses the connection with the kernel.\nThus, you must not evaluate `os.Exit` or functions that call it internally (e.g. `log.Fatal`) in lgo.\n\nIf `os.Exit` is not the reason of \"Dead kernel\", please check crash logs of the kernel.\nIf you run your notebook with `jupyter notebook` command in a terminal, the crash log should be there.\nIf you run your notebook in docker, attach the container's terminal with [`docker attach`](https://docs.docker.com/engine/reference/commandline/attach/) to view the logs.\nIf you can see the logs of `jupyter notebook`, you should see logs like\n\n```\n2018/03/01 20:30:45 lgo-internal failed: exit status 1\n[I 22:34:00.500 NotebookApp] KernelRestarter: restarting kernel (1/5)\nkernel abcd1234-5678-efghi-xxxx-777eeffcccbb restarted\n```\n\nand you can probably see helpful information before `lgo-internal failed` message.\n\n## multiple roots\n### Sympton\nGot an error message like:\n\n```\nmultiple roots $LGOPATH/pkg \u0026\nFailed to build a shared library of github.com/yunabe/lgo/sess7b..7d/exec1: exit status 1\n```\n\n### Solutions\nThis error occurs when the `go` command you are currently using is different from the `go` command you used to run `lgo install`.\nFor example, this happens if you update `go` from 1.9 to 1.10 but did not run `lgo install --clean` with the new `go` after the update.\n\nIf you encouter this issue, please double-check that you are using `go` which you used to run `lgo install` to install packages into `$LGOPATH`.\n\n## old export format no longer supported\n### Symptom\nGot error messages like:\n\n```\ncould 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))\n```\n\n### Reason and Solution\nSome libraries installed in your `$GOPATH` are in the old format, which are built go1.6 or before.\nMake sure all libraries under your `$GOPATH` are recompiled with your current go compiler.\n\n```\ncd $GOPATH/src; go install ./...\n```\n","funding_links":[],"categories":["Go","Software Packages","软件包","Jupyter内核","Go Tools","Go 工具","Other Software"],"sub_categories":["Other Software","其他软件","其他软件库和软件包"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyunabe%2Flgo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyunabe%2Flgo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyunabe%2Flgo/lists"}