-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,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: | ||
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. FFI should be in caps 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. or maybe in graves, i guess it's a library here, not the concept |
||
|
||
```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 | ||
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. without the http:// etc I don't think this actually makes a link 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. (and is probably better to turn "the ffi module" into a link with this as the target 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. So that URI isn't actually an HTTP URL: it's the go get-able URL. If you go to it with a web browser it breaks. I can certainly link to https://bitbucket.org/binet/go-ffi/ in the markdown instead of repeating the go-gettable URL however. |
||
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. | ||
|
||
<!-- TODO change times to match other times in document --> | ||
```bash | ||
$ go run embed.go | ||
``` | ||
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 |
||
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, | ||
|
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.
Mercurial should be capitalized