Skip to content

Commit

Permalink
all: add an example
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Feb 27, 2021
1 parent d85adb4 commit f020c25
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
# code2img [![PkgGoDev](https://pkg.go.dev/badge/golang.design/x/code2img)](https://pkg.go.dev/golang.design/x/code2img) ![](https://changkun.de/urlstat?mode=github&repo=golang-design/code2img)

a carbon service wrapper
a carbon service wrapper for Go users

```go
import "golang.design/x/code2img"
```

## API Usage

Just one API `code2img.Render`, to use it:
Just one API `code2img.Render`, to use it (see [main.go](./example/main.go)):

```go
b, err := code2img.Render(context.TODO(), `import "golang.design/x/code2img"`)
if err != nil {
panic(err)
}

os.WriteFile("code.png", b, os.ModePerm)
```
![](./example/code.png)

## Service Usage

Expand Down
2 changes: 1 addition & 1 deletion code2img_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestRender(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

got, err := code2img.Render(ctx, `import "golang.design/x/code2img`)
got, err := code2img.Render(ctx, `import "golang.design/x/code2img"`)
if err != nil {
t.Fatalf("render failed: %v", err)
}
Expand Down
Binary file added example/code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"context"
"os"

"golang.design/x/code2img"
)

func main() {
f, err := os.ReadFile("main.go")
if err != nil {
panic(err)
}
code := string(f)

// render it!
b, err := code2img.Render(context.TODO(), code)
if err != nil {
panic(err)
}

os.WriteFile("code.png", b, os.ModePerm)
}
Binary file removed testdata/got.png
Binary file not shown.
Binary file modified testdata/want.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f020c25

Please sign in to comment.