-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add go to rust-inside-other-languages chapter #28915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
PeterSP
wants to merge
3
commits into
rust-lang:master
from
PeterSP:Add-go-to-rust-inside-other-languages-chapter
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,60 @@ 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](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 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. | ||
|
||
<!-- TODO change times to match other times in document --> | ||
```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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be a newline between the code block and the text |
||
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, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be a newline between the code block and the text