Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Guard ¶
type Guard[T any] struct { // contains filtered or unexported fields }
Guard is a dead simple json marshal guard
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/Sora233/guard"
)
func main() {
var response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data guard.Guard[struct {
Content string `json:"content"`
}] `json:"data"`
}
var body = `{"code":10000, "msg":"xx error", "data": "wrong format"}`
err := json.Unmarshal([]byte(body), &response)
if err != nil {
fmt.Println("can not unmarshal body", err.Error())
return
}
if response.Data.IsSuccess() {
fmt.Println("content is", response.Data.Get().Content)
} else {
fmt.Println("can not get content because of wrong body data", string(response.Data.GetRaw()))
}
}
Output:
func (*Guard[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json unmarshal interface
Click to show internal directories.
Click to hide internal directories.