Skip to content

Add Go language guide#77

Merged
kate-goldenring merged 4 commits intobytecodealliance:mainfrom
kate-goldenring:go-language-guide
Apr 9, 2024
Merged

Add Go language guide#77
kate-goldenring merged 4 commits intobytecodealliance:mainfrom
kate-goldenring:go-language-guide

Conversation

@kate-goldenring
Copy link
Collaborator

@kate-goldenring kate-goldenring commented Sep 23, 2023

Add go language guide and a description of reactor vs command components as a lot of tooling refers to the two but we haven't explicitly called out the difference anywhere

fixes #37

@kate-goldenring
Copy link
Collaborator Author

Currently debugging why the resultant add.component.wasm is not compatible with our example-host. I am getting:

Error: Failed to instantiate the example world

Caused by:
    0: import `wasi:io/streams` has the wrong type
    1: instance export `check-write` has the wrong type
    2: expected func found nothing

@kate-goldenring
Copy link
Collaborator Author

updating wasmtime version did the trick so this is ready for review

Copy link
Collaborator

@itowlson itowlson left a comment

Choose a reason for hiding this comment

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

Thanks for doing this! The spine of it is great, but reading it I sometimes felt a bit adrift. Key suggestions would be:

  1. A bit more of a roadmap up front. We can't go directly from busting out some Go to having a component, so here are the waypoints we're going to hit.
  2. A bit more what and why, around things like the init, SetExample stuff.

But the core story is great! So good to have this in here.

@@ -0,0 +1,89 @@
# Go Tooling
Copy link
Collaborator

Choose a reason for hiding this comment

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

The comment on the PR mentions you needed Wasmtime 13 for this to work. Should we state that on this section, or wait for #78 to land and rev the entire book to a Wasmtime 13-compatible toolchain? (Are there other tools will not work with Wasmtime 13 though? I am not sure what the alignment status is now the Wasmcon freeze is over.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think a full book rev makes sense. I think it is better than leaving at "latest dev" since that already implies Wasmtime 13 changes

wit-bindgen tiny-go ./add.wit -w example --out-dir=gen
```

Now, we can implement the generated `SetExample` type (in `gen/example.go`) that is exported by our component:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is SetExample a name that wit-bindgen creates from... the -w parameter? I'm not quite sure what is happening here.


func init() {
a := AddImpl{}
SetExample(a)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh! I see! (I think.) This is a gadget wit-bindgen gives us for working around the main problem - it's like for registering your interface implementations in a reactor scenario. Something like that?

Copy link
Member

Choose a reason for hiding this comment

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

The reasoning behind using init() is to register user implementation as exported interfaces / functions.

```go
package main

//go:generate wit-bindgen tiny-go ./add.wit -w example --out-dir=gen
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like the line we just ran. What is its role here?

Copy link
Collaborator Author

@kate-goldenring kate-goldenring Oct 2, 2023

Choose a reason for hiding this comment

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

This informs go generate what to run, so for future WIT changes, just go generate needs to be run rather than the explicit wit-bindgen command. But it probably over complicates so ill remove

Copy link
Member

@Mossaka Mossaka left a comment

Choose a reason for hiding this comment

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

This looks great to me! Thank you for your work @kate-goldenring

@rylev
Copy link
Collaborator

rylev commented Mar 21, 2024

@kate-goldenring what's the state of this PR? Looks like at the very least it needs a rebase.

Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
@kate-goldenring
Copy link
Collaborator Author

@itowlson do you mind giving this another review? I broke the guide up into two sections:

  1. a quickstart that just exports an add function
  2. one that uses wit-bindgen to support more complex worlds
    I also call out how a lot of the wasm-tools commands to componentize with be unnecessary once the wasip2 target is added.

…nterface

Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
Copy link
Collaborator

@itowlson itowlson left a comment

Choose a reason for hiding this comment

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

Couple of things to sanity-check but looks good!

3. Convert the Wasm core module to a component using
[]`wasm-tools`](https://github.com/bytecodealliance/wasm-tools)

The next section will walk through steps 1-2, producing a core Wasm module that targets WASI preview 1.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we line up the ordering here - this next section is actually step 2 not step 1 I think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I updated the guide to number the headings and added a section introducing the example world. What do you think? 2be771c

Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
Copy link
Collaborator

@itowlson itowlson left a comment

Choose a reason for hiding this comment

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

This is great - thank you Kate!

@kate-goldenring kate-goldenring merged commit e209a6f into bytecodealliance:main Apr 9, 2024
@kate-goldenring kate-goldenring deleted the go-language-guide branch April 9, 2024 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Language Guide: Add (tiny)go

4 participants