Skip to content

Instantly share code, notes, and snippets.

@cespare
Created October 1, 2012 23:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cespare/3815154 to your computer and use it in GitHub Desktop.
Save cespare/3815154 to your computer and use it in GitHub Desktop.
A first hello-world attempt at pygments.go
package main
// #cgo CFLAGS: -I/usr/include/python2.7
// #cgo LDFLAGS: -lpython2.7
// #include <Python.h>
// int pyRunString(const char *s) { return PyRun_SimpleString(s); }
import "C"
import "unsafe"
var pythonCode = `
from pygments import highlight
from pygments.lexers import RubyLexer
from pygments.formatters import HtmlFormatter
code = 'puts "Hello World"'
print highlight(code, RubyLexer(), HtmlFormatter())
`
func main() {
C.Py_Initialize()
cs := C.CString(pythonCode)
C.pyRunString(cs)
C.free(unsafe.Pointer(cs))
C.Py_Finalize()
}
// $ go run test.go
// <div class="highlight"><pre><span class="nb">puts</span> <span class="s2">&quot;Hello World&quot;</span>
// </pre></div>
package main
// Unfortunately go-python doesn't seem to quite install correctly. If I use it I'll probably fork and fix the CGO library path stuff.
// Installation:
// $ CGO_CFLAGS="-I/usr/include/python2.7" CGO_LDFLAGS="-lpython2.7 -L/usr/lib" go get -a -v bitbucket.org/binet/go-python/pkg/python
import "bitbucket.org/binet/go-python/pkg/python"
var pythonCode = `
from pygments import highlight
from pygments.lexers import RubyLexer
from pygments.formatters import HtmlFormatter
code = 'puts "Hello World"'
print highlight(code, RubyLexer(), HtmlFormatter())
`
func main() {
python.PyRun_SimpleString(pythonCode)
}
// Same output as first program
@sevki
Copy link

sevki commented Nov 25, 2013

This is working for me if you are still looking for pygments in go you should give it a try https://github.com/sevki/sandman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment