mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-29 09:21:03 +02:00
Add example usage as library in README (#560)
Add example usage as library in README
This commit is contained in:
parent
b00b56c335
commit
dde866ffbb
44
README.md
44
README.md
@ -34,6 +34,50 @@ brew install go-jsonnet
|
|||||||
- id: jsonnet-lint
|
- id: jsonnet-lint
|
||||||
```
|
```
|
||||||
|
|
||||||
|
It can also be embedded in your own Go programs as a library:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/google/go-jsonnet"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
vm := jsonnet.MakeVM()
|
||||||
|
|
||||||
|
snippet := `{
|
||||||
|
person1: {
|
||||||
|
name: "Alice",
|
||||||
|
welcome: "Hello " + self.name + "!",
|
||||||
|
},
|
||||||
|
person2: self.person1 { name: "Bob" },
|
||||||
|
}`
|
||||||
|
|
||||||
|
jsonStr, err := vm.EvaluateAnonymousSnippet("example1.jsonnet", snippet)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(jsonStr)
|
||||||
|
/*
|
||||||
|
{
|
||||||
|
"person1": {
|
||||||
|
"name": "Alice",
|
||||||
|
"welcome": "Hello Alice!"
|
||||||
|
},
|
||||||
|
"person2": {
|
||||||
|
"name": "Bob",
|
||||||
|
"welcome": "Hello Bob!"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Build instructions (go 1.11+)
|
## Build instructions (go 1.11+)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
Loading…
x
Reference in New Issue
Block a user