Extends the last digit with zero. struct + html/template

Hi,

I have a Struct:

2017/05/15 17:50:05 main.go:207: {0 2 444 2017-06-15 00:00:00 +0000 UTC}

The value 444 is a float64. Next parse the struct with html/template with this function:

func executeXcontent(w http.ResponseWriter, r *http.Request, templ string, p Daten) string {

	myhtml := getXcontentHtml(w, r, templ)
	t, err := template.New("tmpl").Parse(string(myhtml))
	checkError(err)

	buf := &bytes.Buffer{}

	if err := t.Execute(buf, p); err != nil {
		return err.Error()
	}

	if len(p.GXUmsatzPositionen) > 0 {
		log.Println(p.GXUmsatzPositionen[0].Start[0])
	}

	log.Println(buf.String())

	return buf.String()
}

This works not correctly:

<input type="number" name="Start[0].Jahresabsatz" class="form-control idisabled text-right" value="4440">

The value it is expanded with a zero???

What is the line in your template that corresponds to the produced line?

<input type="number" name="Start[0].Jahresabsatz" class="form-control idisabled text-right" value="{{range .Start}}{{.Jahresabsatz}}{{end}}">

I think there are two values in .Start, the first one has 444 for .Jahreabsatz and the second has 0 (the zero value for float64). These are converted to strings and then placed next to each other without any spaces leaving 4440. This can be quickly checked by putting some text between {{.Jahresabsatz}} and {{end}}.

yes

<input type="number" name="Start[0].Jahresabsatz" class="form-control idisabled text-right" value="444testtest0testtest">

What the "§%$§%&

end now?

If you only want the first one, use {{with (index .Start 0)}}{{.Jahresabsatz}}{{end}}".

I unterstand

the same function on other place

give back: <input type="number" name="Brutto" class="form-control text-right idisabled" value="10.12" placeholder="0">

???

Are you trying to fill in form values for every element in .Start?

Ok, there is a .Start loop [{0 1 222 2017-06-15 00:00:00 +0000 UTC} {0 0 0 2017-06-15 00:00:00 +0000 UTC}]

I do not understand how the empty entry is created

Look in your go code where .Start gets filled in. The second one’s time value is not the zero value, so I suspect the data loaded into .Start has that second entry.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.