Passing range of data to HTML template - go

I have inserted dummy data into the database.
The sample print function prints 13 and its square as per the code in the console. But I need to pass the entire data to the HTML template(index) where I can see a table of all the numbers passed with their respective square number.
How to pass this data in index HTML?
Number x Number = SquareNumber
1 x 1 = 1
2 x 2 = 4
3 x 3 = 9
...
and so on.
func main() {
db, err := sql.Open("mysql", "root:#/godb")
if err != nil {
panic(err.Error())
}
defer db.Close()
stmtIns, err := db.Prepare("INSERT INTO squarenum VALUES(?, ?, ? )") // ? =
placeholder
if err != nil {
panic(err.Error()) }
defer stmtIns.Close()
stmtOut, err := db.Prepare("SELECT squareNum FROM squareNum WHERE number =
?")
if err != nil {
panic(err.Error())
}
defer stmtOut.Close()
for i := 1; i < 50; i++ {
_, err = stmtIns.Exec(0,i, (i * i))
if err != nil {
panic(err.Error())
}
}
err = stmtOut.QueryRow(13).Scan(&squareNum) // WHERE number = 13
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
tRes:=pageData{}
tRes.SquareNum=squareNum
fmt.Printf("The square number of 13 is: %d \n", squareNum)
// Query another number.. 1 maybe?
err = stmtOut.QueryRow(1).Scan(&squareNum) // WHERE number = 1
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
fmt.Printf("The square number of 1 is: %d \n", squareNum)
http.HandleFunc("/",idx)
http.ListenAndServe(":8888", nil)
}
func idx(w http.ResponseWriter, r *http.Request) {
pd := pageData{
SquareNum: squareNum,
}
err := tpl.ExecuteTemplate(w, "index.html", pd)
if err != nil {
log.Println("LOGGED", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}

package main
import (
"html/template"
"os"
)
// here is your template
const tplString = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{{.Title}}</title>
</head>
<body>
{{range .Dummies}}
<div>Square of {{.Number}} is {{.Square}}</div>
{{end}}
</body>
</html>
`
var (
tpl *template.Template
err error
)
// the dummydata you talked about
type DummyData struct {
Number int
Square int
}
//some PageData just with dummies and a title
type PageData struct {
Title string
Dummies []*DummyData
}
//here you would be using your sql queries
func createSomeDummies(amount int) []*DummyData {
dummies := make([]*DummyData, amount)
for i := 0; i < amount; i++ {
dd := new(DummyData)
dd.Number = i
dd.Square = i * i
dummies[i] = dd
}
return dummies
}
func main() {
pd := new(PageData)
pd.Title = "Hello Dummies"
pd.Dummies = createSomeDummies(10)
tpl = template.New("index")
tpl, err = tpl.Parse(tplString)
if err != nil {
panic(err)
}
err = tpl.Execute(os.Stdout, pd)
if err != nil {
panic(err)
}
}
This Snippet creates a PageData struct to hold the array of dummydata entries and a Title for the webpage.
type PageData struct {
Title string
Dummies []*DummyData
}
It then uses a function to create 10 dummydata structs.
This array is then assigned to the Dummmies field of the PageData.
pd.Dummies = createSomeDummies(10)
This function is a placeholder for your sql query function you just have to loop over your sql rows instead of manually creating them like i do.
func createSomeDummies(amount int) []*DummyData {
dummies := make([]*DummyData, amount)
for i := 0; i < amount; i++ {
dd := new(DummyData)
dd.Number = i
dd.Square = i * i
dummies[i] = dd
}
return dummies
}
The Template itself inserts the title like so:
<title>{{.Title}}</title>
The Dummies themselves are inserted by a range template directive that works like a for iterator.
The one thing to be careful about is that inside this loop all data points to the DummyData item and not the PageData
{{range .Dummies}}
<div>Square of {{.Number}} is {{.Square}}</div>
{{end}}
Then the template is parsed. Errors will halt execution and print the error message.
tpl = template.New("index")
tpl, err = tpl.Parse(tplString)
if err != nil {
panic(err)
}
And finally the template will be rendered to Stdout. For use in a http Handler you would have to use the http.ResponseWriter instead.
Once again errors halt execution and print the error message.
err = tpl.Execute(os.Stdout, pd)
if err != nil {
panic(err)
}
Working Example here:
Go Playground

Related

Go: Templates Embedded in Binary Return Blank Page

Trying to move my golang html templates from files to using embed
Works fine:
func loadTemplates() multitemplate.Render {
r := multitemplate.New()
layouts, err := filepath.Glob("templates/layouts/*.tmpl")
if err != nil {
panic(err.Error())
}
includes, err := filepath.Glob("templates/includes/*.tmpl")
if err != nil {
panic(err.Error())
}
// Generate our templates map from our layouts/ and includes/ directories
for _, layout := range layouts {
files := append(includes, layout)
r.Add(filepath.Base(layout), template.Must(template.ParseFiles(files...)))
log.Println(filepath.Base(layout) + ": " + files[0])
}
return r
}
Very similar code returns blank page, no errors:
//go:embed templates/*
var f embed.FS
func loadTemplates() multitemplate.Render {
r := multitemplate.New()
// Generate our templates map from our layouts/ and includes/ directories
layouts, err := embed.FS.ReadDir(f, "templates/layouts")
if err != nil {
panic(err.Error())
}
for _, layout := range layouts {
embeddedTemplate, err := template.ParseFS(f, "templates/layouts/"+layout.Name(), "templates/includes/base.tmpl")
if err != nil {
log.Println(err)
}
r.Add(layout.Name(), embeddedTemplate)
log.Println(layout.Name() + " loaded")
}
return r
}
I confirmed in the debugger that all templates contain no errors and their respective content. Other embedded files such as static assets work fine and get served ok. Even other templates loaded from a database work fine. Just those from embed end up blank.
Any hints what's happening here?
Thanks!
Edit: Full example:
main.go
package main
import (
"embed"
"html/template"
"log"
"path/filepath"
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
)
//go:embed templates/*
var f embed.FS
func main() {
router := gin.Default()
router.HTMLRender = loadTemplates()
router.GET("/embed", HomeHandlerEmbed(router))
router.GET("/file", HomeHandlerFile(router))
router.Run(":8080")
}
func loadTemplates() multitemplate.Render {
r := multitemplate.New()
//load same template from embed FS
embeddedTemplate, err := template.ParseFS(f, "templates/layouts/home.tmpl", "templates/includes/base.tmpl")
if err != nil {
log.Println(err)
}
r.Add("homeEmbed.tmpl", embeddedTemplate)
log.Println("homeEmbed.tmpl" + " loaded from embed FS")
// load same template from real file system
layoutsFile, err := filepath.Glob("templates/layouts/*.tmpl")
if err != nil {
panic(err.Error())
}
includes, err := filepath.Glob("templates/includes/*.tmpl")
if err != nil {
panic(err.Error())
}
for _, layout := range layoutsFile {
files := append(includes, layout)
r.Add(filepath.Base(layout), template.Must(template.ParseFiles(files...)))
log.Println(filepath.Base(layout) + ": " + files[0])
}
return r
}
func HomeHandlerEmbed(r *gin.Engine) gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
c.HTML(200, "homeEmbed.tmpl", nil)
})
}
func HomeHandlerFile(r *gin.Engine) gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
c.HTML(200, "home.tmpl", nil)
})
}
templates/includes/base.tmpl
<!DOCTYPE html>
<html>
<head>
{{template "head" .}}
</head>
<body>
{{template "body" .}}
</body>
</html>
templates/layouts/home.tmpl
{{define "head"}}<title>Test</title>{{end}}
{{define "body"}}
Body
{{end}}
/file works fine, /embed comes up blank
In function loadTemplates() just fix this line:
embeddedTemplate, err := template.ParseFS(f, "templates/includes/base.tmpl", "templates/layouts/home.tmpl")
In your example patterns will be presented in this sequence:
first: "templates/layouts/home.tmpl"
second: "templates/includes/base.tmpl"
But if I understood correctly, the sequence of patterns is important for the function template.ParseFS, because base.tmpl will be included in all you templates.
The function template.ParseFS reads the templates in the process and tries to generate them.

Parsing string to time with unknown layout

I'm having a csv file, and want to read:
Header names
Fields types
So, I wrote the below:
package main
import (
"encoding/csv"
"fmt"
"os"
"log"
"reflect"
"strconv"
)
func main() {
filePath := "./file.csv"
headerNames := make(map[int]string)
headerTypes := make(map[int]string)
// Load a csv file.
f, _ := os.Open(filePath)
// Create a new reader.
r := csv.NewReader(f)
// Read first row only
header, err := r.Read()
checkError("Some other error occurred", err)
// Add mapping: Column/property name --> record index
for i, v := range header {
headerNames[i] = v
}
// Read second row
record, err := r.Read()
checkError("Some other error occurred", err)
// Check record fields types
for i, v := range record {
var value interface{}
if value, err = strconv.Atoi(v); err != nil {
if value, err = strconv.ParseFloat(v, 64); err != nil {
if value, err = strconv.ParseBool(v); err != nil {
if value, err = strconv.ParseBool(v); err != nil { // <== How to do this with unknown layout
// Value is a string
headerTypes[i] = "string"
value = v
fmt.Println(reflect.TypeOf(value), reflect.ValueOf(value))
} else {
// Value is a timestamp
headerTypes[i] = "time"
fmt.Println(reflect.TypeOf(value), reflect.ValueOf(value))
}
} else {
// Value is a bool
headerTypes[i] = "bool"
fmt.Println(reflect.TypeOf(value), reflect.ValueOf(value))
}
} else {
// Value is a float
headerTypes[i] = "float"
fmt.Println(reflect.TypeOf(value), reflect.ValueOf(value))
}
} else {
// Value is an int
headerTypes[i] = "int"
fmt.Println(reflect.TypeOf(value), reflect.ValueOf(value))
}
}
for i, _ := range header {
fmt.Printf("Header: %v \tis\t %v\n", headerNames[i], headerTypes[i])
}
}
func checkError(message string, err error) {
// Error Logging
if err != nil {
log.Fatal(message, err)
}
}
And with csv file as:
name,age,developer
"Hasan","46.4","true"
I got an output as:
Header: name is string
Header: age is float
Header: developer is bool
The output is correct.
The thing that I could not do is the one is checking if the field is string as I do not know what layout the field could be.
I aware I can pasre string to time as per the format stated at https://go.dev/src/time/format.go, and can build a custom parser, something like:
test, err := fmtdate.Parse("MM/DD/YYYY", "10/15/1983")
if err != nil {
panic(err)
}
But this will work only (as per my knowledge) if I know the layout?
So, again my question is, how can I parse time, or what shall I do to be able to parse it, if I do not know the layout?
Thanks to the comment by Burak, I found the solution by using this package: github.com/araddon/dateparse
// Normal parse. Equivalent Timezone rules as time.Parse()
t, err := dateparse.ParseAny("3/1/2014")
// Parse Strict, error on ambigous mm/dd vs dd/mm dates
t, err := dateparse.ParseStrict("3/1/2014")
> returns error
// Return a string that represents the layout to parse the given date-time.
layout, err := dateparse.ParseFormat("May 8, 2009 5:57:51 PM")
> "Jan 2, 2006 3:04:05 PM"

Go HTML template

I have created a simple scraper that takes the top 10 news from a website and returns a JSON with the title and the score. I want to pass the title and the score as HTML template so I can generate a webpage. I'm not familiar with the templating Go language and I don't know how to pass the values for each of the links. Here is the HTML code that I should use and my implementation for now:
<!DOCTYPE html>
<html>
<head><linkrel="stylesheet" href="https://unpkg.com/mvp.css"
/>
</head>
<body>
<h1>{{.PageTitle}}</h1>
<ul>
{{range .Links}}
<li>{{.Title}}: {{.Score}}</li>
{{end}}
</ul>
</body>
</html>
My code:
package main
import (
"encoding/json"
"html/template"
"log"
"net/http"
"strconv"
)
type TopStories struct {
Title string `json:"title"`
Score int `json:"score"`
}
type TopStoriesPayload struct {
TopStories []TopStories
}
type NewsScraper struct {
url string
Data []TopStories
}
type templateData struct {
PageTitle string
Data []TopStories
}
func NewNewsScraper(url string) *NewsScraper {
return &NewsScraper{url: url}
}
func Top10Stories() []string {
req, err := http.NewRequest("GET", "https://hacker-news.firebaseio.com/v0/topstories.json", nil)
if err != nil {
log.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
var IDs []int
json.NewDecoder(resp.Body).Decode(&IDs)
IDs = IDs[:10]
var IDsString []string
for _, id := range IDs {
IDsString = append(IDsString, strconv.Itoa(id))
}
return IDsString
}
func (n *NewsScraper) GetTopStories() {
req, err := http.NewRequest("GET", n.url, nil)
if err != nil {
log.Fatal(err)
}
for _, id := range Top10Stories() {
req.URL.Path = "/v0/item/" + id + ".json"
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
var topStory TopStories
json.NewDecoder(resp.Body).Decode(&topStory)
n.Data = append(n.Data, topStory)
}
}
//create html template handler for top stories
func HTMLHandler(w http.ResponseWriter, r *http.Request) {
scraper := NewNewsScraper("https://hacker-news.firebaseio.com")
scraper.GetTopStories()
tmpl:= template.Must(template.ParseFiles("template.html"))
data := templateData{
PageTitle: "Top Stories",
Data :[]TopStories{
//what should I put here?
},
}
tmpl.Execute(w, data)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/top", HTMLHandler)
http.ListenAndServe(":8080", mux)
}
I see three issues with your code:
a) The template.html file should have space between link & rel
<linkrel="stylesheet" href="https://unpkg.com/mvp.css"/>
to
<link rel="stylesheet" href="https://unpkg.com/mvp.css"/>
b) The template.html file should contain .Data instead of .Links.
c) The go code should be replaced from the below
Data :[]TopStories{
//what should I put here?
},
to
Data : scraper.Data,

How to get DOM HTML in Go

I'm writing a parser HTML in Go. I need to get HTML and pass it to another function.
I did it so:
Can`t pass "doc" to another function
receivedURL, err := http.Get("http://lavillitacafe.com/")
doc, err := goquery.NewDocumentFromReader(receivedURL.Body)
//"linkScrape" this is another function
contactURL := linkScrape(doc)
and
HTML is transferred in parts to another function.
resp, err := http.Get("http://lavillitacafe.com/")
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
for true {
bs := make([]byte, 1014)
n, err := resp.Body.Read(bs)
contactURL := linkScrape(bs[:n])
if n == 0 || err != nil{
break
}
}
How do I do it right?
Here's the basic goquery example adjusted to your use case:
package main
import (
"fmt"
"log"
"strings"
"github.com/PuerkitoBio/goquery"
)
func findHeader(d *goquery.Document) string {
header := d.Find("h1").Text()
return header
}
func main() {
// create from a string
data := `
<html>
<head>
<title>My document</title>
</head>
<body>
<h1>Header</h1>
</body>
</html>`
doc, err := goquery.NewDocumentFromReader(strings.NewReader(data))
if err != nil {
log.Fatal(err)
}
fmt.Println(findHeader(doc))
}

Go Runtime error: “assignment to entry in nil map”

I'm new in go lang. I'm trying to read csv file and collecting data.
But after run it I got this error :
panic: assignment to entry in nil map
goroutine 1 [running]:
panic(0x4dedc0, 0xc082002440)
C:/Go/src/runtime/panic.go:464 +0x3f4
main.(*stateInformation).setColumns(0xc08202bd40, 0xc082060000, 0x11, 0x20)
F:/Works/Go/src/examples/state-info/main.go:25 +0xda
main.main()
F:/Works/Go/src/examples/state-info/main.go:69 +0xaea
My code :
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"strconv"
)
type stateInformation struct {
columns map[string]int
}
type state struct {
id int
name string
abbreviation string
censusRegionName string
}
func (info *stateInformation) setColumns(record []string) {
for idx, column := range record {
info.columns[column] = idx
}
}
func (info *stateInformation) parseState(record []string) (*state, error) {
column := info.columns["id"]
id, err := strconv.Atoi(record[column])
if err != nil {
return nil, err
}
name := record[info.columns["name"]]
abbreviation := record[info.columns["abbreviation"]]
censusRegionName := record[info.columns["census_region_name"]]
return &state{
id: id,
name: name,
abbreviation: abbreviation,
censusRegionName: censusRegionName,
}, nil
}
func main() {
// #1 open a file
f, err := os.Open("state_table.csv")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
stateLookup := map[string]*state{}
info := &stateInformation{}
// #2 parse a csv file
csvReader := csv.NewReader(f)
for rowCount := 0; ; rowCount++ {
record, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatalln(err)
}
if rowCount == 0 {
info.setColumns(record)
} else {
state, err := info.parseState(record)
if err != nil {
log.Fatalln(err)
}
stateLookup[state.abbreviation] = state
}
}
// state-information AL
if len(os.Args) < 2 {
log.Fatalln("expected state abbreviation")
}
abbreviation := os.Args[1]
state, ok := stateLookup[abbreviation]
if !ok {
log.Fatalln("invalid state abbreviation")
}
fmt.Println(`
<html>
<head></head>
<body>
<table>
<tr>
<th>Abbreviation</th>
<th>Name</th>
</tr>`)
fmt.Println(`
<tr>
<td>` + state.abbreviation + `</td>
<td>` + state.name + `</td>
</tr>
`)
fmt.Println(`
</table>
</body>
</html>
`)
}
What's wrong in my code?
I don't know what you are trying to obtain, but the error tells, that columns map does not have a column index on the moment of assignment and for this reason is throwing a panic.
panic: assignment to entry in nil map
To make it work you have to initialize the map itself before to start to populate with indexes.
state := &stateInformation{
columns: make(map[string]int),
}
Or another way to initialize:
func (info *stateInformation) setColumns(record []string) {
info.columns = make(map[string]int)
for idx, column := range record {
info.columns[column] = idx
}
}

Resources