package
Version:
v0.0.0-...-71a7f5d
Opens a new window with list of versions in this module.
Published: May 10, 2024
License: Apache-2.0
Opens a new window with license information.
Imports: 4
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
Documentation
¶
type JsonColumn[T any] struct {
Val T
Valid bool
}
JsonColumn 代表存储字段的 json 类型
主要用于没有提供默认 json 类型的数据库
T 可以是结构体,也可以是切片或者 map
一切可以被 json 库所处理的类型都能被用作 T
func (*JsonColumn[T]) Scan ¶
Scan 将 src 转化为对象
src 的类型必须是 []byte, *[]byte, string, sql.RawBytes, *sql.RawBytes 之一
js := JsonColumn[User]{}
err := js.Scan(`{"Name":"Tom"}`)
if err != nil {
fmt.Println(err)
}
fmt.Print(js.Val)
Output:
{Tom}
func (JsonColumn[T]) Value ¶
Value 返回一个 json 串。类型是 []byte
js := JsonColumn[User]{Valid: true, Val: User{Name: "Tom"}}
value, err := js.Value()
if err != nil {
fmt.Println(err)
}
fmt.Print(string(value.([]byte)))
Output:
{"Name":"Tom"}
Source Files
¶
Click to show internal directories.
Click to hide internal directories.