非常教程

Go参考手册

文本 | text

text/template/parse

  • import "text/template/parse"
  • 概观
  • 索引

概观

解析构包建为 text / template和html / template 定义的模板分析树。客户应该使用这些软件包来构建模板,而不是这个软件包,它提供了不用于一般用途的共享内部数据结构。

索引

  • func IsEmptyTree(n Node) bool
  • func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]interface{}) (map[string]*Tree, error)
  • type ActionNode
  • func (a *ActionNode) Copy() Node
  • func (a *ActionNode) String() string
  • type BoolNode
  • func (b *BoolNode) Copy() Node
  • func (b *BoolNode) String() string
  • type BranchNode
  • func (b *BranchNode) Copy() Node
  • func (b *BranchNode) String() string
  • type ChainNode
  • func (c *ChainNode) Add(field string)
  • func (c *ChainNode) Copy() Node
  • func (c *ChainNode) String() string
  • type CommandNode
  • func (c *CommandNode) Copy() Node
  • func (c *CommandNode) String() string
  • type DotNode
  • func (d *DotNode) Copy() Node
  • func (d *DotNode) String() string
  • func (d *DotNode) Type() NodeType
  • type FieldNode
  • func (f *FieldNode) Copy() Node
  • func (f *FieldNode) String() string
  • type IdentifierNode
  • func NewIdentifier(ident string) *IdentifierNode
  • func (i *IdentifierNode) Copy() Node
  • func (i *IdentifierNode) SetPos(pos Pos) *IdentifierNode
  • func (i *IdentifierNode) SetTree(t *Tree) *IdentifierNode
  • func (i *IdentifierNode) String() string
  • type IfNode
  • func (i *IfNode) Copy() Node
  • type ListNode
  • func (l *ListNode) Copy() Node
  • func (l *ListNode) CopyList() *ListNode
  • func (l *ListNode) String() string
  • type NilNode
  • func (n *NilNode) Copy() Node
  • func (n *NilNode) String() string
  • func (n *NilNode) Type() NodeType
  • type Node
  • type NodeType
  • func (t NodeType) Type() NodeType
  • type NumberNode
  • func (n *NumberNode) Copy() Node
  • func (n *NumberNode) String() string
  • type PipeNode
  • func (p *PipeNode) Copy() Node
  • func (p *PipeNode) CopyPipe() *PipeNode
  • func (p *PipeNode) String() string
  • type Pos
  • func (p Pos) Position() Pos
  • type RangeNode
  • func (r *RangeNode) Copy() Node
  • type StringNode
  • func (s *StringNode) Copy() Node
  • func (s *StringNode) String() string
  • type TemplateNode
  • func (t *TemplateNode) Copy() Node
  • func (t *TemplateNode) String() string
  • type TextNode
  • func (t *TextNode) Copy() Node
  • func (t *TextNode) String() string
  • type Tree
  • func New(name string, funcs ...map[string]interface{}) *Tree
  • func (t *Tree) Copy() *Tree
  • func (t *Tree) ErrorContext(n Node) (location, context string)
  • func (t *Tree) Parse(text, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]interface{}) (tree *Tree, err error)
  • type VariableNode
  • func (v *VariableNode) Copy() Node
  • func (v *VariableNode) String() string
  • type WithNode
  • func (w *WithNode) Copy() Node

文件包

lex.go node.go parse.go

func IsEmptyTreeSource

func IsEmptyTree(n Node) bool

IsEmptyTree 报告此树(节点)是否为空而不是空间。

func ParseSource

func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]interface{}) (map[string]*Tree, error)

Parse 从模板名称返回映射到 parse.Tree,通过解析参数字符串中描述的模板创建。顶层模板将被赋予指定的名称。如果遇到错误,解析将停止并返回一个空映射并显示错误。

键入ActionNodeSource

ActionNode 包含一个动作(由分隔符限定)。控制动作有自己的节点;ActionNode 表示简单的例如字段评估和圆括号的管道。

type ActionNode struct {
        NodeType
        Pos

        Line int       // The line number in the input. Deprecated: Kept for compatibility.
        Pipe *PipeNode // The pipeline in the action.
        // contains filtered or unexported fields
}

func (*ActionNode) CopySource

func (a *ActionNode) Copy() Node

func (*ActionNode) StringSource

func (a *ActionNode) String() string

键入BoolNodeSource

BoolNode 保存一个布尔常量。

type BoolNode struct {
        NodeType
        Pos

        True bool // The value of the boolean constant.
        // contains filtered or unexported fields
}

func (*BoolNode) CopySource

func (b *BoolNode) Copy() Node

func (*BoolNode) StringSource

func (b *BoolNode) String() string

键入BranchNodeSource

BranchNode 是 if,range 和 with 的常见表示。

type BranchNode struct {
        NodeType
        Pos

        Line     int       // The line number in the input. Deprecated: Kept for compatibility.
        Pipe     *PipeNode // The pipeline to be evaluated.
        List     *ListNode // What to execute if the value is non-empty.
        ElseList *ListNode // What to execute if the value is empty (nil if absent).
        // contains filtered or unexported fields
}

func (*BranchNode) CopySource

func (b *BranchNode) Copy() Node

func (*BranchNode) StringSource

func (b *BranchNode) String() string

type ChainNodeSource

ChainNode 包含一个术语,后跟一串字段访问(标识符以'。'开头)。这些名字可能被链接('.x.y')。期间从每个身份证中删除。

type ChainNode struct {
        NodeType
        Pos

        Node  Node
        Field []string // The identifiers in lexical order.
        // contains filtered or unexported fields
}

func (*ChainNode) AddSource

func (c *ChainNode) Add(field string)

添加将指定的字段(应以句点开头)添加到链的末尾。

func (*ChainNode) CopySource

func (c *ChainNode) Copy() Node

func (*ChainNode) StringSource

func (c *ChainNode) String() string

键入CommandNodeSource

CommandNode 包含一个命令(一个评估操作中的管道)。

type CommandNode struct {
        NodeType
        Pos

        Args []Node // Arguments in lexical order: Identifier, field, or constant.
        // contains filtered or unexported fields
}

func (*CommandNode) CopySource

func (c *CommandNode) Copy() Node

func (*CommandNode) StringSource

func (c *CommandNode) String() string

type DotNodeSource

DotNode 包含特殊标识符'。'。

type DotNode struct {
        NodeType
        Pos
        // contains filtered or unexported fields
}

func (*DotNode) CopySource

func (d *DotNode) Copy() Node

func (*DotNode) StringSource

func (d *DotNode) String() string

func (*DotNode) TypeSource

func (d *DotNode) Type() NodeType

type FieldNodeSource

FieldNode 包含一个字段(标识符以'。'开始)。这些名字可能被链接('.x.y')。该期限从每个身份证件中删除。

type FieldNode struct {
        NodeType
        Pos

        Ident []string // The identifiers in lexical order.
        // contains filtered or unexported fields
}

func (*FieldNode) CopySource

func (f *FieldNode) Copy() Node

func (*FieldNode) StringSource

func (f *FieldNode) String() string

键入IdentifierNodeSource

IdentifierNode 包含一个标识符。

type IdentifierNode struct {
        NodeType
        Pos

        Ident string // The identifier's name.
        // contains filtered or unexported fields
}

func NewIdentifierSource

func NewIdentifier(ident string) *IdentifierNode

NewIdentifier 用给定的标识符名称返回一个新的 IdentifierNode。

func (*IdentifierNode) CopySource

func (i *IdentifierNode) Copy() Node

func (*IdentifierNode) SetPosSource

func (i *IdentifierNode) SetPos(pos Pos) *IdentifierNode

SetPos 设置位置。NewIdentifier 是一种公共方法,所以我们不能修改它的签名。为了方便链接。TODO:修好一天?

func (*IdentifierNode) SetTreeSource

func (i *IdentifierNode) SetTree(t *Tree) *IdentifierNode

SetTree 设置节点的父树。NewIdentifier 是一种公共方法,所以我们不能修改它的签名。为了方便链接。TODO:修好一天?

func (*IdentifierNode) StringSource

func (i *IdentifierNode) String() string

type IfNodeSource

IfNode 表示一个 {{if}} 动作及其命令。

type IfNode struct {
        BranchNode
}

func (*IfNode) CopySource

func (i *IfNode) Copy() Node

type ListNodeSource

ListNode 包含一系列节点。

type ListNode struct {
        NodeType
        Pos

        Nodes []Node // The element nodes in lexical order.
        // contains filtered or unexported fields
}

func (*ListNode) CopySource

func (l *ListNode) Copy() Node

func (*ListNode) CopyListSource

func (l *ListNode) CopyList() *ListNode

func (*ListNode) StringSource

func (l *ListNode) String() string

type NilNodeSource

NilNode 保存表示无类型 nil 常量的特殊标识符'nil'。

type NilNode struct {
        NodeType
        Pos
        // contains filtered or unexported fields
}

func (*NilNode) CopySource

func (n *NilNode) Copy() Node

func (*NilNode) StringSource

func (n *NilNode) String() string

func (*NilNode) TypeSource

func (n *NilNode) Type() NodeType

type NodeSource

节点是分析树中的一个元素。界面很简单。该接口包含一个未导出的方法,因此只有该包的本地类型才能满足它。

type Node interface {
        Type() NodeType
        String() string
        // Copy does a deep copy of the Node and all its components.
        // To avoid type assertions, some XxxNodes also have specialized
        // CopyXxx methods that return *XxxNode.
        Copy() Node
        Position() Pos // byte position of start of node in full original input string
        // contains filtered or unexported methods
}

键入NodeTypeSource

NodeType 标识分析树节点的类型。

type NodeType int
const (
        NodeText    NodeType = iota // Plain text.
        NodeAction                  // A non-control action such as a field evaluation.
        NodeBool                    // A boolean constant.
        NodeChain                   // A sequence of field accesses.
        NodeCommand                 // An element of a pipeline.
        NodeDot                     // The cursor, dot.

        NodeField      // A field or method name.
        NodeIdentifier // An identifier; always a function name.
        NodeIf         // An if action.
        NodeList       // A list of Nodes.
        NodeNil        // An untyped nil constant.
        NodeNumber     // A numerical constant.
        NodePipe       // A pipeline of commands.
        NodeRange      // A range action.
        NodeString     // A string constant.
        NodeTemplate   // A template invocation action.
        NodeVariable   // A $ variable.
        NodeWith       // A with action.
)

func (NodeType) TypeSource

func (t NodeType) Type() NodeType

Type 返回自身并提供一个简单的默认实现来嵌入节点。嵌入在所有非平凡的节点中。

键入NumberNodeSource

NumberNode 包含一个数字:有符号或无符号整数,浮点数或复数。该值被解析并存储在所有可以表示该值的类型下。这在少量代码中模拟 Go 的理想常量的行为。

type NumberNode struct {
        NodeType
        Pos

        IsInt      bool       // Number has an integral value.
        IsUint     bool       // Number has an unsigned integral value.
        IsFloat    bool       // Number has a floating-point value.
        IsComplex  bool       // Number is complex.
        Int64      int64      // The signed integer value.
        Uint64     uint64     // The unsigned integer value.
        Float64    float64    // The floating-point value.
        Complex128 complex128 // The complex value.
        Text       string     // The original textual representation from the input.
        // contains filtered or unexported fields
}

func (*NumberNode) CopySource

func (n *NumberNode) Copy() Node

func (*NumberNode) StringSource

func (n *NumberNode) String() string

键入PipeNodeSource

PipeNode 拥有一个可选声明的管道

type PipeNode struct {
        NodeType
        Pos

        Line int             // The line number in the input. Deprecated: Kept for compatibility.
        Decl []*VariableNode // Variable declarations in lexical order.
        Cmds []*CommandNode  // The commands in lexical order.
        // contains filtered or unexported fields
}

func (*PipeNode) CopySource

func (p *PipeNode) Copy() Node

func (*PipeNode) CopyPipeSource

func (p *PipeNode) CopyPipe() *PipeNode

func (*PipeNode) StringSource

func (p *PipeNode) String() string

键入PosSource

Pos 表示从中解析模板的原始输入文本中的字节位置。

type Pos int

func (Pos) PositionSource

func (p Pos) Position() Pos

键入RangeNodeSource

RangeNode表示一个 {{range}} 动作及其命令。

type RangeNode struct {
        BranchNode
}

func (*RangeNode) CopySource

func (r *RangeNode) Copy() Node

键入StringNodeSource

StringNode 包含一个字符串常量。该值已被“未引用”。

type StringNode struct {
        NodeType
        Pos

        Quoted string // The original text of the string, with quotes.
        Text   string // The string, after quote processing.
        // contains filtered or unexported fields
}

func (*StringNode) CopySource

func (s *StringNode) Copy() Node

func (*StringNode) StringSource

func (s *StringNode) String() string

键入TemplateNodeSource

TemplateNode 表示一个 {{template}} 动作。

type TemplateNode struct {
        NodeType
        Pos

        Line int       // The line number in the input. Deprecated: Kept for compatibility.
        Name string    // The name of the template (unquoted).
        Pipe *PipeNode // The command to evaluate as dot for the template.
        // contains filtered or unexported fields
}

func (*TemplateNode) CopySource

func (t *TemplateNode) Copy() Node

func (*TemplateNode) StringSource

func (t *TemplateNode) String() string

键入TextNodeSource

TextNode 保存纯文本。

type TextNode struct {
        NodeType
        Pos

        Text []byte // The text; may span newlines.
        // contains filtered or unexported fields
}

func (*TextNode) CopySource

func (t *TextNode) Copy() Node

func (*TextNode) StringSource

func (t *TextNode) String() string

键入TreeSource

树是单个解析模板的表示。

type Tree struct {
        Name      string    // name of the template represented by the tree.
        ParseName string    // name of the top-level template during parsing, for error messages.
        Root      *ListNode // top-level root of the tree.
        // contains filtered or unexported fields
}

func NewSource

func New(name string, funcs ...map[string]interface{}) *Tree

New 分配给定名称的新分析树。

func (*Tree) CopySource

func (t *Tree) Copy() *Tree

Copy 返回树的副本。任何解析状态都会被丢弃。

func (*Tree) ErrorContextSource

func (t *Tree) ErrorContext(n Node) (location, context string)

ErrorContext 返回输入文本中节点位置的文本表示。只有当节点没有指向内部树的指针时才会使用接收器,这可能会发生在旧代码中。

func (*Tree) ParseSource

func (t *Tree) Parse(text, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]interface{}) (tree *Tree, err error)

Parse 解析模板定义字符串以构建用于执行的模板表示。如果任何动作分隔符字符串为空,则使用默认值(“{{”或“}}”)。嵌入式模板定义被添加到树形图。

键入VariableNodeSource

VariableNode 包含变量名称列表,可能带有链接字段访问。美元符号是(第一)名称的一部分。

type VariableNode struct {
        NodeType
        Pos

        Ident []string // Variable name and fields in lexical order.
        // contains filtered or unexported fields
}

func (*VariableNode) CopySource

func (v *VariableNode) Copy() Node

func (*VariableNode) StringSource

func (v *VariableNode) String() string

键入WithNodeSource

WithNode 表示一个 {{with}} 动作及其命令。

type WithNode struct {
        BranchNode
}

func (*WithNode) CopySource

func (w *WithNode) Copy() Node
Go

Go 是一种编译型语言,它结合了解释型语言的游刃有余,动态类型语言的开发效率,以及静态类型的安全性。它也打算成为现代的,支持网络与多核计算的语言。要满足这些目标,需要解决一些语言上的问题:一个富有表达能力但轻量级的类型系统,并发与垃圾回收机制,严格的依赖规范等等。这些无法通过库或工具解决好,因此Go也就应运而生了。

主页 https://golang.org/
源码 https://go.googlesource.com/go
发布版本 1.9.2

Go目录

1.档案 | archive
2.缓冲区 | bufio
3.内置 | builtin
4.字节 | bytes
5.压缩 | compress
6.容器 | container
7.上下文 | context
8.加密 | crypto
9.数据库 | database
10.调试 | debug
11.编码 | encoding
12.错误 | errors
13. expvar
14.flag
15. fmt
16. go
17.散列 | hash
18.html
19.图像 | image
20.索引 | index
21.io
22.日志 | log
23.数学 | math
24. math/big
25.math/bits
26.math/cmplx
27.math/rand
28.拟态 | mime
29.net
30.net/http
31. net/mail
32. net/rpc
33.net/smtp
34. net/textproto
35. net/url
36.os
37.路径 | path
38.插件 | plugin
39.反射 | reflect
40.正则表达式 | regexp
41.运行时 | runtime
42.排序算法 | sort
43.转换 | strconv
44.字符串 | strings
45.同步 | sync
46.系统调用 | syscall
47.测试 | testing
48.文本 | text
49.时间戳 | time
50.unicode
51.不安全性 | unsafe
52.Go 语言数据类型
53.Go 语言基础语法
54.Go 语言结构
55.Go 语言 select 语句
56.Go 语言 switch 语句
57.Go 语言 if 语句嵌套
58.Go 语言 if…else 语句
59.Go 语言 if 语句
60.Go 语言运算符
61.Go 语言常量
62.Go 语言函数闭包
63.Go 语言函数作为实参
64.Go 语言函数引用传递值
65.Go 语言函数值传递值
66.Go 语言函数
67.Go 语言 goto 语句
68.Go 语言 continue 语句
69.Go 语言 break 语句
70.Go 语言循环嵌套
71.Go 语言 for 循环
72.Go 语言结构体
73.Go 语言指针作为函数参数
74.Go 语言指向指针的指针
75.Go 语言指针数组
76.Go 语言指针
77.Go 语言向函数传递数组
78.Go 语言多维数组
79.Go 语言变量作用域
80.Go 语言函数方法
81.Go 错误处理
82.Go 语言接口
83.Go 语言类型转换
84.Go 语言递归函数
85.Go 语言Map(集合)
86.Go 语言范围(Range)
87.Go 语言切片(Slice)
88.Go 并发
89.Go fmt.Sprintf 格式化字符串