Skip to content

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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion src/doc/trpl/rust-inside-other-languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
```
Copy link
Member

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


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
Copy link
Member

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

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,
Expand Down