GitHub - pope/ob-go: Org-Babel support for evaluating go code.
source link: https://github.com/pope/ob-go
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
README.org
Readme
Introduction
ob-go
enables Org-Babel support for evaluating go code. It was
created based on the usage of ob-C. The go code is compiled and run
via the go run
command. If a main
function isn’t present, by
default the code is wrapped in a simple main
func. If :package
option isn’t set and no package is declared in the code, then the
main
package is declared.
#+begin_src go :imports "fmt" fmt.Println("Hello, 世界") #+end_src
:
#+results: : Hello, 世界
Language Specific Header Arguments
In addition to the normal header arguments for Babel, below are some some headers specific to go.
:args
Command line arguments to pass to the executable compiled from
the code block. To pass more than one argument, use a list.
:flags
Flags to pass to the go run
command. These are the flags
that you would pass to go build
.
:main
If set to no
, inhibits the auto wrapping of the main
function call. Default: yes
:imports
Shorthand for supplying imports to the app. This should be
used when you’re letting the application handle the main
function. To supply more, use a list.
:package
Set the package of the file. Requires :main no. If
not set, and code doesn’t have a explicit package, then main
package is declared.
:var
`ob-go’ also supports Babel variables with some limitations. See
`ob-go’ for more information about some of the limitations using
:var
.
Additional Examples
Multiple Imports
#+begin_src go :imports '("fmt" "time") fmt.Println("Current Time:", time.Now()) #+end_src
:
#+RESULTS: : Current Time: 2012-04-29 11:47:36.933733 -0700 PDT
Concurrent Prime Sieve
#+begin_src go // A concurrent prime sieve package main
:
import "fmt"
:
// Send the sequence 2, 3, 4, ... to channel 'ch'. func Generate(ch chan<- int) { for i := 2; ; i++ { ch <- i // Send 'i' to channel 'ch'. } }
:
// Copy the values from channel 'in' to channel 'out', // removing those divisible by 'prime'. func Filter(in <-chan int, out chan<- int, prime int) { for { i := <-in // Receive value from 'in'. if i%prime != 0 { out <- i // Send 'i' to 'out'. } } }
:
// The prime sieve: Daisy-chain Filter processes. func main() { ch := make(chan int) // Create a new channel. go Generate(ch) // Launch Generate goroutine. for i := 0; i < 10; i++ { prime := <-ch fmt.Println(prime) ch1 := make(chan int) go Filter(ch, ch1, prime) ch = ch1 } } #+end_src
:
#+RESULTS: #+begin_example 2 3 5 7 11 13 17 19 23 29 #+end_example
Running tests
Tests can be executed by make test or invoking emacs directly with the command-line below:
# For Emacs earlier than 24, add -L /path/to/ert emacs -Q --batch \ -L . \ -l ob-go.el \ -l test-ob-go.el \ --eval "(progn \ (setq org-confirm-babel-evaluate nil) \ (org-babel-do-load-languages \ 'org-babel-load-languages '((emacs-lisp . t) \ (sh . t) \ (org . t) \ (go . t))))" \ -f ob-go-test-runall
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK