From ca96cad8c67d6676452418c0a3bf495a6b4a9104 Mon Sep 17 00:00:00 2001 From: PeterSP Date: Thu, 8 Oct 2015 15:22:07 -0400 Subject: [PATCH 1/3] Add go to rust-inside-other-languages chapter Like ruby, python, and node.js javascript, go is a commonly used modern language on the server. Like them it is garbage-collected. Unlike them, it does not have a GIL (indeed, it is even compiled to native assembly instructions). A quick conversation in the Rust IRC channel lends me to think this is at least worth considering. --- src/doc/trpl/rust-inside-other-languages.md | 54 ++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/doc/trpl/rust-inside-other-languages.md b/src/doc/trpl/rust-inside-other-languages.md index 47e1df37dffb7..a4da2f1027acb 100644 --- a/src/doc/trpl/rust-inside-other-languages.md +++ b/src/doc/trpl/rust-inside-other-languages.md @@ -23,7 +23,7 @@ that extra oomph. There is a whole [chapter devoted to FFI][ffi] and its specifics elsewhere in the book, but in this chapter, we’ll examine this particular use-case of FFI, -with examples in Ruby, Python, and JavaScript. +with examples in Ruby, Python, JavaScript, and Go. [ffi]: ffi.html @@ -336,6 +336,58 @@ print the result. On my system, this takes a quick `0.092` seconds. +# Go + +Go may not have a GIL, but there are cases where you may need to +avoid its garbage collector. + +In order to do FFI in Go, we first need to download the library: + +```bash +$ go get bitbucket.org/binet/go-ffi/pkg/ffi +``` + +(You may need to install mercurial first). + +After it's installed, we can use ffi: + +```go +package main + +import "bitbucket.org/binet/go-ffi/pkg/ffi" +import "fmt" + +func main() { + lib, _ := ffi.NewLibrary("target/release/libembed.so") + process, _ := lib.Fct("process", ffi.Void, []ffi.Type{}) + process() + fmt.Println("done!") +} +``` + +This also looks a great deal like the Ruby and Node examples. +We use the `ffi` module obtained from +bitbucket.org/binet/go-ffi/pkg/ffi to get `ffi.NewLibrary()`, which +loads our shared object library. We have to state the return type +and argument types o fthe function, which are `ffi.Void` for return +and an empty array of `ffi.Type` type to mean no arguments. However, +here we have two choices: Executing with `go run` and compiling with +`go build` and then running the generated binary. + + +```bash +$ go run embed.go +``` +Will compile and run our example, and takes about `0.250s` on my +system. + +```bash +$ go build embed.go +$ ./embed +``` +Will compile and run our example in separate commands. Timing just +execution, this takes an impressive `0.002s` on my system. + # Conclusion As you can see, the basics of doing this are _very_ easy. Of course, From 31a0dfa56dd4103dbfa99ba15afbb45d6c46a2bc Mon Sep 17 00:00:00 2001 From: PeterSP Date: Sat, 10 Oct 2015 01:47:54 -0400 Subject: [PATCH 2/3] Improving per pull request comments Capitalization, spacing, and link neatness. --- src/doc/trpl/rust-inside-other-languages.md | 36 +++++++++++---------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/doc/trpl/rust-inside-other-languages.md b/src/doc/trpl/rust-inside-other-languages.md index a4da2f1027acb..af95128afeba1 100644 --- a/src/doc/trpl/rust-inside-other-languages.md +++ b/src/doc/trpl/rust-inside-other-languages.md @@ -347,37 +347,38 @@ In order to do FFI in Go, we first need to download the library: $ go get bitbucket.org/binet/go-ffi/pkg/ffi ``` -(You may need to install mercurial first). +(You may need to install Mercurial first). -After it's installed, we can use ffi: +After it's installed, we can use `ffi`: ```go -package main +package main -import "bitbucket.org/binet/go-ffi/pkg/ffi" -import "fmt" +import "bitbucket.org/binet/go-ffi/pkg/ffi" +import "fmt" -func main() { - lib, _ := ffi.NewLibrary("target/release/libembed.so") - process, _ := lib.Fct("process", ffi.Void, []ffi.Type{}) - process() - fmt.Println("done!") +func main() { + lib, _ := ffi.NewLibrary("target/release/libembed.so") + process, _ := lib.Fct("process", ffi.Void, []ffi.Type{}) + process() + fmt.Println("done!") } ``` This also looks a great deal like the Ruby and Node examples. -We use the `ffi` module obtained from -bitbucket.org/binet/go-ffi/pkg/ffi to get `ffi.NewLibrary()`, which -loads our shared object library. We have to state the return type -and argument types o fthe function, which are `ffi.Void` for return -and an empty array of `ffi.Type` type to mean no arguments. However, -here we have two choices: Executing with `go run` and compiling with -`go build` and then running the generated binary. +We use [the `ffi` module](https://bitbucket.org/binet/go-ffi/) to get +`ffi.NewLibrary()`, which loads our shared object library. We have to +state the return type and argument types of the function, which are +`ffi.Void` for return and an empty array of `ffi.Type` type to mean +no arguments. However, here we have two choices: Executing with +`go run` and compiling with `go build` and then running the generated +binary. ```bash $ go run embed.go ``` + Will compile and run our example, and takes about `0.250s` on my system. @@ -385,6 +386,7 @@ system. $ go build embed.go $ ./embed ``` + Will compile and run our example in separate commands. Timing just execution, this takes an impressive `0.002s` on my system. From ee991e211c227f6683c5b3c2ecea802058c9bbb5 Mon Sep 17 00:00:00 2001 From: PeterSP Date: Sat, 10 Oct 2015 02:23:10 -0400 Subject: [PATCH 3/3] Minor wording touchup --- src/doc/trpl/rust-inside-other-languages.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/doc/trpl/rust-inside-other-languages.md b/src/doc/trpl/rust-inside-other-languages.md index af95128afeba1..93ba957a61650 100644 --- a/src/doc/trpl/rust-inside-other-languages.md +++ b/src/doc/trpl/rust-inside-other-languages.md @@ -369,10 +369,10 @@ This also looks a great deal like the Ruby and Node examples. We use [the `ffi` module](https://bitbucket.org/binet/go-ffi/) to get `ffi.NewLibrary()`, which loads our shared object library. We have to state the return type and argument types of the function, which are -`ffi.Void` for return and an empty array of `ffi.Type` type to mean -no arguments. However, here we have two choices: Executing with -`go run` and compiling with `go build` and then running the generated -binary. +`ffi.Void` for return and an empty array with element type `ffi.Type` +to mean no arguments. However, here we have two choices: Executing +with `go run` and compiling with `go build` and then running the +generated binary. ```bash