xkuokuo / ob-go

Org-Babel support for evaluating go code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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. As a result of my efforts, now `ob-go` can handle table and list . Maybe not elegant, but really can work.

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

Table and List

#+NAME: tb-phone-book
| name  |    tel |
|-------+--------|
| brant | 170... |
| ou    | 138... |
#+BEGIN_SRC go :imports "fmt"  :var tb=tb-phone-book
  fmt.Println(tb)
  fmt.Println(tb_helper(1, "name"))
#+END_SRC
#+RESULTS:
: [[brant 170...] [ou 138...]]
: ou
#+BEGIN_SRC go :imports "fmt" :var int_lst='(1 2 3)  float_lst='(1.0 2.2 3.3) string_lst='("a" "b" "c")
  fmt.Println(int_lst)
  fmt.Println(float_lst)
  fmt.Println(string_lst)
#+END_SRC
#+RESULTS:
: [1 2 3]
: [1 2.2 3.3]
: [a b c]

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

About

Org-Babel support for evaluating go code.


Languages

Language:Emacs Lisp 97.5%Language:Makefile 2.5%