Coder Social home page Coder Social logo

ob-go's Introduction

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

ob-go's People

Contributors

brantou avatar caseneuve avatar hlissner avatar i4ki avatar juergenhoetzel avatar micanzhang avatar pope avatar syohex avatar wallyqs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ob-go's Issues

void function =string-to-int=

I'm using Emacs version from source code repository with branch master. and Org-mode version from source code branch master.

And workaround is here:

(if (not (fboundp 'string-to-int)) ; used in `ob-go.el'
      (defalias 'string-to-int 'string-to-number))

Symbol's function definition is void: org-babel--get-vars

I am using Emacs 25 (compiled from source) and org-mode (release-8.3.4)

When I try code block as below:

#+BEGIN_SRC go
  fmt.Println("Hello World!")
#+END_SRC

I got error: "Symbol's function definition is void: org-babel--get-vars"

When I install org-mode from master branch, it works fine.

Support importing external packages

It would be convenient if it were possible to also import packages not part of the standard library, e.g., via…

#+begin_src go :imports "golang.org/x/exp/constraints@latest"
…
#+end_src

table input is not supported

Can I start working on it? Is there work in progress, or can I start with the latest in git?
My thoughts are between simple (multi-string variable using ` and interpret the s-expression in go) and complex (setup an array of structs with named fields). Any directions for me please!

go 1.17 to loose GOPATH support

With go 1.17 loosing GOPATH support. It may be necessary to generate a temporary module rather than a simple go file when executing the code block.

problem with evaluating result

(if-let ((results
	      (org-babel-eval
	       (format "%s run %s \"%s\" %s"
		       org-babel-go-command
		       (mapconcat 'identity (org-babel-go-as-list flags) " ")
		       (org-babel-process-file-name tmp-src-file)
		       (mapconcat #'(lambda (a)
				      ;; If there's a chance that the symbol is a
				      ;; ref, use that. Otherwise, just return the
				      ;; string form of the value.
				      (format "%S" (if (symbolp a)
						       (let* ((ref (symbol-name a))
							      (out (org-babel-read ref)))
							 (if (equal out ref)
							     (if (string-match "^\".*\"$" ref)
								 (read ref)
							       (org-babel-ref-resolve ref))
							   out))
						     a)))
				  (org-babel-go-as-list args) " ")) "")))

I encountered an "Invalid function" error when I evaluated results, then I find "if-let" in this code block in file "ob-go.el".I guess "if-let" is invalid in my emacs,so I change it into "let",and it works! Finally I find "if-let" is a macro in new Emacs.

error with package

when i wrote org like this:

+BEGIN_SRC go

package main

import "fmt"

func main() {
fmt.Println("Hello World!")
}

+END_SRC

and execute this code, i got:

package main: 
/var/folders/m0/l7hfbwvn3dz9qnx_dxf65vvm0000gp/T/babel-31761XsV/go-src-31761BqN.go:2:1: expected 'package', found 'import'

so, i check out generated go source hinted by above, and i found:


import (

)


package main
import "fmt"

func main() {
    fmt.Println("Hello World!")
}%

is it a bug or my problem?

Don't know how to use

althought I add ob-go from melpa, but it doesn't work well. could you tell me how to config it in emacs
Thank you

feature request: support import's in source with main() auto-wrapping enabled

It will be handy if ob-go detected import statements and put them at the beginning of the generated file, without needing to explicitly declare a main() function, so:

import "fmt"
fmt.Println(1)

will be internally translated to:

package main
import "fmt"
func main() {
    fmt.Println(1)
}

Currently this block is translated to invalid go and the compilation fails:

package main
func main() {
    import "fmt"
    fmt.Println(1)
}

print err message at results section

i prefer to print err message at results section, e.p:

#+BEGIN_SRC go :imports "fmt"
func main() {
    fmt.Println("Hello ", name)
}
#+END_SRC

#+RESULTS:
# command-line-arguments
/var/folders/m0/l7hfbwvn3dz9qnx_dxf65vvm0000gp/T/babel-31761XsV/go-src-31761Dez.go:8: undefined: name

Support the ability for :package to be set to none

I am trying to tangle an org file consisting of go code blocks which are all tangled into one file. This package works pretty well besides the inability to set :package to none. For instance, when I untangle I set it so that :main no is a default argument for source blocks but there is no ability to set :package to an empty string or nil leading to the resulting tangled file being filled with various 'package main' lines all over for each source block. Adding this would definitely be useful for overall literate go programming with the package.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.