Documentation
¶
Overview ¶
Package content 提供对各类媒体数据的处理
Index ¶
- Constants
- Variables
- func AcceptCharset(header string) (name string, enc encoding.Encoding, err error)
- func AcceptLanguage(cl catalog.Catalog, header string) language.Tag
- func BuildContentType(mt, charset string) string
- func CharsetIsNop(enc encoding.Encoding) bool
- func ParseContentType(v string) (mimetype, charset string, err error)
- type MarshalFunc
- type Mimetypes
- func (mt *Mimetypes) Add(name string, m MarshalFunc, u UnmarshalFunc) error
- func (mt *Mimetypes) ConentType(header string) (UnmarshalFunc, encoding.Encoding, error)
- func (mt *Mimetypes) Delete(name string)
- func (mt *Mimetypes) Marshal(header string) (string, MarshalFunc, error)
- func (mt *Mimetypes) Set(name string, m MarshalFunc, u UnmarshalFunc) error
- func (mt *Mimetypes) Unmarshal(name string) (UnmarshalFunc, error)
- type UnmarshalFunc
Constants ¶
const DefaultCharset = "utf-8"
DefaultCharset 默认的字符集
const DefaultMimetype = "application/octet-stream"
DefaultMimetype 默认的媒体类型
在不能获取输入和输出的媒体类型时, 会采用此值作为其默认值。
若编码函数中指定该类型的函数,则会使用该编码优先匹配 */* 等格式的请求。
Variables ¶
var ( // ErrNotFound 表示未找到指定名称的编解码函数 // // 在 Mimetypes.Marshal 和 Mimetypes.Unmarshal 中会返回该错误。 ErrNotFound = errors.New("未找到指定名称的编解码函数") // ErrExists 存在相同中名称的编解码函数 // // 在 Mimetypes.AddMarshal 和 Mimetypes.AddUnmarshal 时如果已经存在相同名称,返回此错误。 ErrExists = errors.New("已经存在相同名称的编解码函数") )
var Nil *struct{}
Nil 表示向客户端输出 nil 值
这是一个只有类型但是值为空的变量。在某些特殊情况下, 如果需要向客户端输出一个 nil 值的内容,可以使用此值。
Functions ¶
func AcceptCharset ¶
AcceptCharset 根据 Accept-Charset 报头的内容获取其最值的字符集信息
传递 * 获取返回默认的字符集相关信息,即 utf-8 其它值则按值查找,或是在找不到时返回空值。
返回的 name 值可能会与 header 中指定的不一样,比如 gb_2312 会被转换成 gbk
func AcceptLanguage ¶
AcceptLanguage 从 accept-language 报头中获取最适合的本地化语言信息
func ParseContentType ¶
ParseContentType 从 content-type 中获取编码和字符集
若客户端传回的是空值,则会使用默认值代替。
返回值中,mimetype 一律返回小写的值,charset 则原样返回
Types ¶
type MarshalFunc ¶
MarshalFunc 将一个对象转换成 []byte 内容时所采用的接口
type Mimetypes ¶
type Mimetypes struct {
// contains filtered or unexported fields
}
Mimetypes 管理 mimetype 的增删改查
func (*Mimetypes) Add ¶ added in v0.36.0
func (mt *Mimetypes) Add(name string, m MarshalFunc, u UnmarshalFunc) error
Add 添加编解码函数
m 和 u 可以为 nil,表示仅作为一个占位符使用,具体处理要在 ServeHTTP 中另作处理。
func (*Mimetypes) ConentType ¶ added in v0.36.0
ConentType 从 content-type 报头解析出需要用到的解码函数
func (*Mimetypes) Marshal ¶
func (mt *Mimetypes) Marshal(header string) (string, MarshalFunc, error)
Marshal 从 header 解析出当前请求所需要的解 mimetype 名称和对应的解码函数
*/* 或是空值 表示匹配任意内容,一般会选择第一个元素作匹配; xx/* 表示匹配以 xx/ 开头的任意元素,一般会选择 xx/* 开头的第一个元素; xx/ 表示完全匹配以 xx/ 的内容 如果传递的内容如下:
application/json;q=0.9,*/*;q=1
则因为 */* 的 q 值比较高,而返回 */* 匹配的内容
在不完全匹配的情况下,返回值的名称依然是具体名称。
text/*;q=0.9
返回的名称可能是:
text/plain
func (*Mimetypes) Set ¶ added in v0.36.0
func (mt *Mimetypes) Set(name string, m MarshalFunc, u UnmarshalFunc) error
Set 修改编解码函数
type UnmarshalFunc ¶
UnmarshalFunc 将客户端内容转换成一个对象时所采用的接口
Directories
¶
| Path | Synopsis |
|---|---|
|
Package form 用于处理 www-form-urlencoded 编码 func read(ctx *web.Context) { vals := urls.Values{} ctx.Read(vals) } func write(ctx *web.Context) { vals := urls.Values{} vals.Add("name", "caixw") ctx.Render(http.StatusOK, vals, nil) } form 用户可以通过定义 form 标签自定义输出的名称,比如: type Username struct { Name string `form:"name"` Age int } 转换成 form-data 可能是以下样式: name=jjj&age=18 该方式对数据类型有一定限制: 1.
|
Package form 用于处理 www-form-urlencoded 编码 func read(ctx *web.Context) { vals := urls.Values{} ctx.Read(vals) } func write(ctx *web.Context) { vals := urls.Values{} vals.Add("name", "caixw") ctx.Render(http.StatusOK, vals, nil) } form 用户可以通过定义 form 标签自定义输出的名称,比如: type Username struct { Name string `form:"name"` Age int } 转换成 form-data 可能是以下样式: name=jjj&age=18 该方式对数据类型有一定限制: 1. |
|
Package gob 提供 GOB 格式的编解码
|
Package gob 提供 GOB 格式的编解码 |
|
Package html 提供输出 HTML 内容的 content.MarshalFunc 函数 mt := content.NewMimetypes() tpl := template.ParseFiles(...) mgr := html.New(tpl) mt.Add("text/html", mgr.Marshal, nil) func handle(ctx *web.Context) { ctx.Render(200, html.Tpl("index", map[string]interface{}{...}), nil) }
|
Package html 提供输出 HTML 内容的 content.MarshalFunc 函数 mt := content.NewMimetypes() tpl := template.ParseFiles(...) mgr := html.New(tpl) mt.Add("text/html", mgr.Marshal, nil) func handle(ctx *web.Context) { ctx.Render(200, html.Tpl("index", map[string]interface{}{...}), nil) } |
|
Package mimetypetest 针对文本内容的编解码实现,仅作为测试用例。
|
Package mimetypetest 针对文本内容的编解码实现,仅作为测试用例。 |