Original post

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"

)

type HeaderFooter struct {

Header, Footer string

}

type Chunks struct {

SliceChunks  [][]string

SliceRandoms []string

ChunkId      []int

}

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 : %vn", i, m, chunks[i])

}

return chunks

}

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

}

func 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"))

chnk := Chunks{}

headfoot := HeaderFooter{}

type data struct {

    chunk Chunks

    HF    HeaderFooter

}

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("Enter The No Of Users And No Of Users Should Be Less Than : ", len(sliceItems))

fmt.Println("--------------------------------------------------------------------------")

chnk.SliceRandoms = SliceRand(users)

chnk.SliceChunks = SliceChunker(sliceItems, len(chnk.SliceRandoms))

headfoot.Header = "Ukirusha mawe kwa mbwa wengi, atakaye piga nduru ndiye aliye gongwa : The guilty ones are always.."

headfoot.Footer = "He that sleepeth with an itchy ■■■■ wakes up with a smelly finger"

for i := range chnk.SliceChunks {

    chnk.ChunkId = append(chnk.ChunkId, i+1)

}

d := data{

    chunk: chnk,

    HF:    headfoot,

}

tpl.ExecuteTemplate(w, "index.html", d)

fmt.Println(d)

//chnk = entities.Chunks{}

}

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

~

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

{{.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>

~