"range" of actions and "pipelined" explanation in the text / html template package. Golang

I am trying to get some subtleties in the text / html template package. I read his documentation from golang website. It is hard to understand what exactly means. (dot) as a whole and at a certain time in the range. What exactly means "pipeline", maybe it's hard to understand, because my English is not my native language):

{{pipeline}}
The default textual representation of the value of the pipeline
is copied to the output.

Consider an example:

    data := map[string]interface{}{
        "struct": &Order{
            ID:     1,
            CustID: 2,
            Total:  3.65,
            Name: "Something",
        },
        "name1":  "Timur",
        "name2": "Renat",
    }
    t.ExecuteTemplate(rw, "index", data)

Here is the "index":

{{define "index"}}
    {{range $x := .}}
        {{.}}
        <b>{{$x}}</b><br>
        <i>{{$.struct.ID}}</i><br>
        <br>
        # the lines below don't work and break the loop
        # {{.ID}}
        # or
        # {{.struct.ID}}

        # what if I want here another range loop that handles "struct" members
        # when I reach "struct" field in the data variable or just do nothing
        # and just continue the loop? 
    {{end}}
{{end}}

Conclusion:

Timur
Timur
1

Renat
Renat
1

{1 2 3.65 Something}
{1 2 3.65 Something}
1

+4
source share
1

"", .

, , Mac:

route -n get default | grep 'gateway' | awk '{print $2}'

, route -n get default. , , | : " route grep". grep 'gateway' , route. grep awk. , , , , , awk .

. . :

{{ "Hello world!" | printf "%s" }}

{{ printf "%s" "Hello World!" }}

. Go

,

{{ "Hello World!" | printf "%s"           }}
    ^^^^^^^^^^^^               ^^^^^^^^^^
         |__________________________|

( , . F #).

.

"-". , , . , . range, .

. Go

$x . . , .

"struct"

- -... a map. , :

{{ range $key, $value = . }}

, . :

{{ if eq $key "struct" }}
    {{ /* $value.ID is the ID you want */ }}

. Go Playground

, .

+5

All Articles