I need help in ranging over an array and slice in html page. When I try ranging over the arrays and chunks, they are not being displayed in the html page but the header section gets pased over to the html page.
Sorry guys if this is a noob question. Your assistance will be highly appreciated
package main
import (
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
"strconv"
"time"
//entities "deadmeat.pro/chunker/src/entities"
//helpers "deadmeat.pro/chunker/src/helpers"
)
///This is the header footer struct. This is working alright when passed to the template
type HeaderFooter struct {
Header, Footer string
}
///This is the Chunks struct meant to have the sliced chunks and randomized users to be assigned
//to the Slice chunks.
///This is not displaying in the html form.
type Chunks struct {
SliceChunks [][]string
SliceRandoms []string
ChunkId []int
}
//Contains data for both Chunks structs and HeaderFooter structs to be passed into a template
//file
type Data struct{
Chunk Chunks
HF HeaderFooter
}
//This chunks the passed array lists to be chunked into chunks that are equal to the number of
//users.
func SliceChunker(items []string, n int) [][]string {
if n < 0 {
return nil
}
chunks := make([][]string, n)
for i := range chunks {
m := (len(items) + (n - 1)) / n
chunks[i] = items[:m:m]
items = items[m:]
n--
fmt.Printf("CHUNK %2d = %d : %v\n", i, m, chunks[i])
}
return chunks
}
///This randomizes the users so as the chunks assignment to the users is not predetermined but
///random
func SliceRand(sliceItems []string) []string {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(sliceItems), func(i, j int) { sliceItems[i], sliceItems[j] = sliceItems[j], sliceItems[i] })
return sliceItems
}
//The Index page method to pass data into the html template page
func (d Data) Index(w http.ResponseWriter, r *http.Request) {
var tpl *template.Template
tpl = template.Must(template.ParseGlob("form/*.html"))
//template.Must(tpl.ParseGlob("form/register/*.html"))
var sliceItems []string
users := []string{"Hosea", "Clarence Obando", "Mukhongo Simiyu", "Jimmie Carter", "Qabale", "McDonald", "Sierra", "Tyra", "James",
"Kimberly", "Waweru", "Stata", "Karis", "Brigg Symons", "Abdi", "Kuti", "Eliud", "Wolde", "Buno", "Cheptoo Kipsang"}
A2Z := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z"}
for x := 0; x <= 9; x++ {
for _, i := range A2Z {
sliceItems = append(sliceItems, strconv.Itoa(x)+i)
}
}
fmt.Println("--------------------------------------------------------------------------")
d.Chunk.SliceRandoms = helpers.SliceRand(users)
d.Chunk.SliceChunks = helpers.SliceChunker(sliceItems, len(users))
d.HF.Header = "Ukirusha mawe kwa mbwa wengi, atakaye piga nduru ndiye aliye gongwa : The guilty ones are always.."
d.HF.Footer = "He that sleepeth with an itchy ■■■■ wakes up with a smelly finger"
for i := range d.Chunk.SliceChunks {
d.Chunk.ChunkId = append(d.Chunk.ChunkId, i+1)
}
tpl.ExecuteTemplate(w, "index.html", d)
fmt.Println(d)
d = Data{}
fmt.Println("END --->>>>>> = ", d)
}
func main() {
log.Println("Server started on: http://localhost:8080")
http.HandleFunc("/", Index)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
The HTML part which is in a folder called form looks like this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{{.HF.Header}}<br>
<table border="1">
<thead>
<tr>
<td>ID</td>
<td>USER</td>
<td>ITEMS</td>
</tr>
</thead>
<tbody>
{{ range .Chunk }}
<tr>
<td> {{ .ChunkId }} </td>
<td>{{ .SliceRandoms }}</td>
<td> {{ .SliceChunks }} </td>
</tr>
{{ end }}
</tbody>
</table>
<h1>{{.HF.Footer}}</h1><br>
</body>
</html>
The chunks slices and users slices seem not to be passable to the Index page. Upon passing, they are not displaying on the index form.
The footer and header data is displaying fine but the slices seem to be unpassable