1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file contains the code to handle template options.
11 // missingKeyAction defines how to respond to indexing a map with a key that is not present.
12 type missingKeyAction
int
15 mapInvalid missingKeyAction
= iota // Return an invalid reflect.Value.
16 mapZeroValue
// Return the zero value for the map element.
21 missingKey missingKeyAction
24 // Option sets options for the template. Options are described by
25 // strings, either a simple string or "key=value". There can be at
26 // most one equals sign in an option string. If the option string
27 // is unrecognized or otherwise invalid, Option panics.
31 // missingkey: Control the behavior during execution if a map is
32 // indexed with a key that is not present in the map.
33 // "missingkey=default" or "missingkey=invalid"
34 // The default behavior: Do nothing and continue execution.
35 // If printed, the result of the index operation is the string
38 // The operation returns the zero value for the map type's element.
40 // Execution stops immediately with an error.
42 func (t
*Template
) Option(opt
...string) *Template
{
44 for _
, s
:= range opt
{
50 func (t
*Template
) setOption(opt
string) {
52 panic("empty option string")
54 elems
:= strings
.Split(opt
, "=")
61 case "invalid", "default":
62 t
.option
.missingKey
= mapInvalid
65 t
.option
.missingKey
= mapZeroValue
68 t
.option
.missingKey
= mapError
73 panic("unrecognized option: " + opt
)