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

README.md: add fully-worked tiny example, mention it works in tinygo #261

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,50 @@ go generate ./...
```
This is most of what you need to know about go generate, but you can sese more about [go generate on the golang blog](http://blog.golang.org/generate).

## Complete minimal example

Here's a working minimal example. Create demo/demo.go containing the struct to be marshalled, in its own module:
```Go
package demo

//go:generate ffjson -nodecoder demo.go

type AwesomeStruct struct {
Foo string
Bar string
}
```
Then create main.go to marshal and print one of those structs:
```Go
package main

import (
"fmt"
"log"
"example.com/jsondemo/demo"
)

func main() {
foo := demo.AwesomeStruct{
Foo: "buz",
Bar: "",
}
marshaled, err := foo.MarshalJSON()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", marshaled) // => {"foo":"buz"}
}
```
Finally, create a go.mod, add ffjson to it, generate the marshaling code, and run:
```sh
$ go mod init example.com/jsondemo
$ go get -u github.com/pquerna/ffjson
$ (cd demo; go generate)
$ go run main.go
```
This example also works with [tinygo](https://github.com/tinygo-org/tinygo/)!

## Should I include ffjson files in VCS?

That question is really up to you. If you don't, you will have a more complex build process. If you do, you have to keep the generated files updated if you change the content of your structs.
Expand Down