Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSONReader ¶
type JSONReader struct {
// contains filtered or unexported fields
}
func NewJSONReader ¶
func NewJSONReader(val any) *JSONReader
NewJSONReader 用于解决将一个结构体序列化为 JSON 之后,再封装为 io.Reader 的场景。 该实现没有做任何输入检查。 也就是你需要自己确保 val 是一个可以被 json 正确处理的东西。 非线程安全。 如果你传入的是 nil,那么读到的结果应该是 null。务必小心。
Example ¶
package main
import (
"fmt"
"net/http"
"github.com/ecodeclub/ekit/iox"
)
func main() {
val := iox.NewJSONReader(User{Name: "Tom"})
_, err := http.NewRequest(http.MethodPost, "/hello", val)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("OK")
}
type User struct {
Name string `json:"name"`
}
Output:
Click to show internal directories.
Click to hide internal directories.