Nim language bindings for golib - a library that (ab)uses gccgo to bring Go's channels and goroutines to the rest of the world.
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(getproccount()) |
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.
The API is subject to change until the first version (0.0.1) is released. After this, backwards compatibility will become a priority.
- golib
- Nim > 0.19.0
- gcc >= 7.3
./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
nimble install
BSD-2
-
author: Ștefan Talpalaru stefantalpalaru@yahoo.com