Skip to content

Commit

Permalink
Add GO example (#21)
Browse files Browse the repository at this point in the history
I forgot about this. 

Thank you!
  • Loading branch information
Yakiyo committed Jul 16, 2023
1 parent cca509c commit 5b52f05
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [C#](examples/unofficial/csharp.md)
- [C++](examples/unofficial/c++.md)
- [Dart](examples/unofficial/dart.md)
- [Go](examples/unofficial/go.md)
- [Rust](examples/unofficial/rust.md)
<!-- PLEASE ALPHABETICALLY -->

Expand Down
112 changes: 112 additions & 0 deletions src/examples/unofficial/go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<!-- markdownlint-disable MD014 -->

# [![CS](https://cdn.discordapp.com/emojis/1128611804596158574.webp?size=24&quality=lossless)](https://nekos.best/discord?ref=docs) Go

---

## Installation
Add the module to your project
```bash
$ go get -u github.com/Yakiyo/nekos_best.go
```

## Usage
Import it in your code
```go
import (
nb github.com/Yakiyo/nekos_best.go
)
```
Full reference at [go.pkg.dev](https://pkg.go.dev/github.com/Yakiyo/nekos_best.go)

### Fetch a single entry
```go
res, err := nb.Fetch("neko")
if err != nil {
// handle err
}
fmt.Println(res.Url, res.Artist_name, res.Artist_href)
```

### Fetch multiple entries
```go
res, _ := nb.FetchMany("baka", 3)

fmt.Println(res[0].Url)
```

### Fetch a file
```go
import "os"

res, _ := nb.FetchFile("pat")

os.WriteFile("image.png", res.Data, 0644)

// print associated entry for the image
fmt.Println(res.Body)
```

### Search for an entry
```go
res, _ := nb.Search("Gochuumon wa Usagi Desuka??", "baka", 3)

fmt.Println(res)
```

### Generate random category
```go
cat := nb.RandomCategory()
```

## Example without the module
```go
package main

import (
"fmt"
"io"
"net/http"
"encoding/json"
)

// Result of an entry for nekos.best api response for
// an image endpoint
type Result struct {
Url string
Artist_name string
Artist_href string
}

// The entire api response returned
type Api_Response struct {
Results []Result
}

func main() {
res, err := http.Get("https://nekos.best/api/v2/neko")
if err != nil {
fmt.Println("Error %v", err)
return
}
// Read body to bytes and defer the close
defer res.Body.Close()
bytes, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println("Error %v", err)
return
}
// Parse []byte to json
response := &Api_Response{}
json.Unmarshal(bytes, response)
fmt.Println(response.Results[0])
}
```

## About

> Example added by: [**Yakiyo**](https://github.com/Yakiyo)
>
> Package source code is available on [**Github**](https://github.com/Yakiyo/nekos_best.go)
>
> [![Go Reference](https://pkg.go.dev/badge/github.com/Yakiyo/nekos_best.go.svg)](https://pkg.go.dev/github.com/Yakiyo/nekos_best.go) [![NekosBest](https://img.shields.io/github/stars/Yakiyo/nekos_best.go?color=yellow&label=Stars&logo=github&style=flat-square)](https://github.com/Yakiyo/nekos_best.go)

0 comments on commit 5b52f05

Please sign in to comment.