Nim language bindings for golib - a library that (ab)uses gccgo to bring Go's channels and goroutines to the rest of the world
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
benchmarks
src
tests
.gitignore
LICENSE
Makefile.am
README.md
autogen.sh
configure.ac
golib.nimble
nim.cfg.in

README.md

description

Nim language bindings for golib - a library that (ab)uses gccgo to bring Go's channels and goroutines to the rest of the world.

syntax comparison

feature Go Nim
channel type
(here in a
function
parameter)
func S(a, b chan uint) int { proc S(a, b: chan[uint]): int =
restricted
channel types
chan<- float64
<-chan int
send_chan[float64]
recv_chan[int]
create channel c := make(chan int)
c2 := make(chan int, 1)
var c = make_chan(int)
var c2 = make_chan(int, 1)
send value
to channel
c <- 1 c <- 1
receive value
from channel
av := <-a
av, bv := <-a, <-b
cv, ok := <-c
var av = <-a
var (av, bv) = (<-a, <-b)
var (cv, ok) = <--c
iterate
over channel
for av := range a for av in a
channel select select {
case c0 <- 0:
case <-c1:
case i2 = <-c2:
case i3, ok3 = <-c3:
case li[0] = <-c4:
case li[f()] = <-c5:
default:
break LOOP
}
select:
scase c0 <- 0: discard
scase <-c1: discard
scase (i2 = <-c2): discard
scase ((i3, ok3) = <--c3): discard
scase (li[0] = <-c4): discard
scase (li[f()] = <-c5): discard
default:
break LOOP
declare goroutine func f(x, y int) {
println(x, y)
}
proc f(x, y: int) {.goroutine.} =
echo(x, " ", y)
launch goroutine go f(1, 2) go f(1, 2)
lambda goroutine go func(c chan int) { c <- 1 }(r) Unsupported. Workaround:
proc f(c: chan[int]) {.goroutine.} = c <- 1
go f(r)
non-blocking
sleep
time.Sleep(100 * time.Millisecond) go_sleep_ms(100)
yield to another
goroutine
runtime.Gosched() go_yield()
run the goroutines
on all the available
CPU cores
runtime.GOMAXPROCS(runtime.NumCPU()) runtime_gomaxprocsfunc(runtime_ncpu)
special code
layout
import golib

proc go_main() {.gomain.} =
# main code here

golib_main()
# not reached
compiler
parameters
# nim.cfg
--threads:on
--stackTrace:off
--passC:"--std=gnu99 -fsplit-stack"
--dynlibOverride:"go"
--passL:"-lgolib -lgo"
--gc:go
# or --gc:none

The initialization of the Go runtime (including the GC) is done in golib_main(), after which go_main() is launched as a goroutine and the main event loop is started. That's why you can't do heap memory allocation before go_main() runs and also why anything after golib_main() won't be reached - the event loop ends the program when go_main() exits.

API stability

The API is subject to change until the first version (0.0.1) is released. After this, backwards compatibility will become a priority.

requirements

  • golib
  • Nim > 0.17.0
  • gcc >= 6.3

build the benchmark and run tests

./autogen.sh

If you have a Nim repo in ../Nim/:

./configure NIM=../Nim/bin/nim

Or with the system-wide Nim and no GC (some tests and benchmarks will fail without the Go GC):

./configure --disable-gogc

Run tests:

make check

Benchmark (compare it with the ones from golib):

make
/usr/bin/time -v ./benchmarks/cw_nim

install

nimble install

license

BSD-2

credits