Range the map in Go-Template

Hi,

i get data from the Mysql databse

rows, err := db.Query("SELECT * FROM tasks WHERE pid=? AND del=0", pid)
		checkError(err)

		defer rows.Close()
		columns, _ := rows.Columns()
		scanArgs := make([]interface{}, len(columns))
		values := make([]string, len(columns))

		for i, _ := range scanArgs {
			scanArgs[i] = &values[i]
		}

		rs := make(map[int]map[string]string)
		i := 0
		for rows.Next() {
			err := rows.Scan(scanArgs...)
			if err != nil {
				panic(err.Error())
			}

			r := make(map[string]string)
			for k, v := range values {
				r[columns[k]] = v
			}

			rs[i] = r
			i++
		}

		d.Tabledata = rs

and take to the template from struct

type Daten struct {
	Tabledata        map[int]map[string]string
}

my template

  {{ range $key, $element := .Tabledata }}
                    <li><strong>{{ $key }}</strong>: </li>

                    {{range $element}}


                        {{.Value}}
                    {{end}}


                 {{ end }}

i see only the key int from map[]

<li><strong>0</strong>: map[close:0 tid:1 inprocess:0 abnahme:0 beschreibung:description uid:1 del:0 ende:0000-00-00 00:00:00 erstellt:2016-06-23 15:13:09 finisch:0 open:1 finischdatum:2016-06-23 15:52:15 pid:4 start:0000-00-00 00:00:00 namethe name]</li>

   <li><strong>1</strong>: map[finisch:0 open:1 finischdatum:2016-06-23 15:52:15 erstellt:2016-06-23 15:13:09 abnahme:0 start:0000-00-00 00:00:00 name:the name beschreibung:description tid:2 uid:1 del:0 inprocess:0 ende:0000-00-00 00:00:00 pid:4 close:0]</li>

What’s the problem?

I don’t know and it’s hard to say because you don’t show your code in full. I’m fairly sure that output is not from that template fragment and data type, for example. Using the playground to show a runnable example of the problem is a superior way to ask the question. I went ahead and imported what I think you might be doing, and it looks like it’s working as well as I’d expect it:

https://play.golang.org/p/icKGt6lCNb

okay

when i use {{.}} i can see all values as one string. i think this map[int]map[string]string is fail, then my rows also have some int. I should use map[int]interface{}

run teplante/range with interface{} ?

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