0

In https://golang.org/pkg/text/template/#hdr-Actions, it has the following explanation

{{template "name" pipeline}} The template with the specified name is executed with dot set to the value of the pipeline.

what does it mean? what's dot for?

For example, I see the following template code -

{{ define "header" }}
{{ template "top" . }}
{{ template "needs" }}

...

{{ end }}

What is the '.' for following the "top" in the code above?

bcbishop
  • 2,193
  • 3
  • 20
  • 23

1 Answers1

1

The value '.' is the current value or cursor as explained in the third paragraph in the documentation:

Annotations in the template refer to elements of the data structure (typically a field of a struct or a key in a map) to control execution and derive values to be displayed. Execution of the template walks the structure and sets the cursor, represented by a period '.' and called "dot", to the value at the current location in the structure as execution proceeds.

The command {{ template "top" . }} executes the template "top" with dot as the argument. Inside "top", dot is set to the argument.

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242