Skip to content
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

Add Go as an embedding to the book #1481

Merged
merged 3 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ Hello, world!
[Cranelift]: https://github.com/bytecodealliance/wasmtime/blob/master/cranelift/README.md
[embedded]: https://bytecodealliance.github.io/wasmtime/embed.html

## Language Support

You can use Wasmtime from a variety of different languages through embeddings of
the implementation:

* **[Rust]** - the [`wasmtime` crate]
* **[C]** - the [`wasm.h`], [`wasi.h`], and [`wasmtime.h`] headers
* **[Python]** - the [`wasmtime` PyPI package]
* **[.NET]** - the [`Wasmtime` NuGet package]
* **[Go]** - the [wasmtime-go repository]

[Rust]: https://bytecodealliance.github.io/wasmtime/embed-rust.html
alexcrichton marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should point to lang-rust.html now.

[C]: https://bytecodealliance.github.io/wasmtime/examples-c-embed.html
[`wasmtime` crate]: https://crates.io/crates/wasmtime
[`wasm.h`]: https://github.com/WebAssembly/wasm-c-api/blob/master/include/wasm.h
[`wasi.h`]: https://github.com/bytecodealliance/wasmtime/blob/master/crates/c-api/include/wasi.h
[`wasmtime.h`]: https://github.com/bytecodealliance/wasmtime/blob/master/crates/c-api/include/wasmtime.h
[Python]: https://bytecodealliance.github.io/wasmtime/lang-python.html
[`wasmtime` PyPI package]: https://pypi.org/project/wasmtime/
[.NET]: https://bytecodealliance.github.io/wasmtime/lang-dotnet.html
[`Wasmtime` NuGet package]: https://www.nuget.org/packages/Wasmtime
[Go]: https://bytecodealliance.github.io/wasmtime/lang-go.html
[wasmtime-go repository]: https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go

## Documentation

[📚 Read the Wasmtime guide here! 📚][guide]
Expand Down
7 changes: 3 additions & 4 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
- [Debugging](./examples-c-debugging.md)
- [Using multi-value](./examples-c-multi-value.md)
- [Using WebAssembly from your lanugage](./lang.md)
- [Rust](./lang-rust.md)
- [C](./lang-c.md)
- [Python](./lang-python.md)
- [.NET](./lang-dotnet.md)
- [Rust](./lang-rust.md)
- [Go](./lang-go.md)
- [Bash](./lang-bash.md)
- [Using the `wasmtime` CLI](./cli.md)
- [Installation](./cli-install.md)
Expand All @@ -39,9 +41,6 @@
- [C/C++](./wasm-c.md)
- [WebAssembly Text Format (`*.wat`)](./wasm-wat.md)
- [Example: Markdown Parser](./wasm-markdown.md)
- [Embedding Wasmtime](embed.md)
- [Rust API](./embed-rust.md)
- [C/C++ API](./embed-c.md)
- [Stability](stability.md)
- [Release Process](./stability-release.md)
- [Platform Support](./stability-platform-support.md)
Expand Down
4 changes: 0 additions & 4 deletions docs/embed-c.md

This file was deleted.

171 changes: 0 additions & 171 deletions docs/embed-rust.md

This file was deleted.

10 changes: 0 additions & 10 deletions docs/embed.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/examples-c-wasi.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This example shows off how to instantiate a wasm module using WASI imports.
## Wasm Source code

```rust,ignore
{{#include ../examples/wasi/wasm/wasi.c}}
{{#include ../examples/wasi/wasm/wasi.rs}}
sunfishcode marked this conversation as resolved.
Show resolved Hide resolved
```


Expand Down
3 changes: 3 additions & 0 deletions docs/lang-c.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# C

... more coming soon
76 changes: 76 additions & 0 deletions docs/lang-go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Go

Wasmtime [is available as a Go
Module](https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go). This guide
will go over adding Wasmtime to your project, and some provided examples of what
can be done with WebAssembly modules.

Make sure you're using Go 1.12 or later with modules support.

## Getting started and simple example

First up you'll want to start a new module:

```sh
$ mkdir hello-wasm
$ cd hello-wasm
$ go mod init hello-wasm
```

Next, copy this example WebAssembly text module into your project. It exports a
function for calculating the greatest common denominator of two numbers.

```wat
{{#include ../examples/gcd.wat}}
```

Next, we can write our code in `main.go` which reads this file and runs it:

```go
package main

import (
"fmt"
"github.com/bytecodealliance/wasmtime-go"
)

func main() {
store := wasmtime.NewStore(wasmtime.NewEngine())
module, err := wasmtime.NewModuleFromFile(store, "gcd.wat")
check(err)
instance, err := wasmtime.NewInstance(module, []*wasmtime.Extern{})
check(err)

gcd := instance.GetExport("gcd").Func()
val, err := gcd.Call(6, 27)
check(err)
fmt.Printf("gcd(6, 27) = %d\n", val.(int32))
}

func check(err error) {
if err != nil {
panic(err)
}
}
```

And finally we can build and run it:

```sh
$ go run main.go
gcd(6, 27) = 3
```

If this is the output you see, congrats! You've successfully ran your first
WebAssembly code in Go!

## More examples and contributing

The `wasmtime` Go package [lives in its own
repository](https://github.com/bytecodealliance/wasmtime-go) and has a [number
of other more advanced
examples](https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go?tab=doc#pkg-examples)
as well. Feel free to browse those, but if you find anything missing don't
hesitate to [open an
issue](https://github.com/bytecodealliance/wasmtime-go/issues/new) and let us
know if you have any questions!
Loading