I'm using html/template to serve html but I'm not sure if I'm using it correctly. I've only pasted the relevant code below (not complete):
This is my Go code:
func homehandler(w http.ResponseWriter, r *http.Request) {
userName := getUserName(r) //this is defined somewhere
if userName != "" {
t := template.Must(template.New("Tele").Parse(homePage))
t.Execute(w, ReadData()) //ReadData() is a function that reads from the MySQL database and returns a string array
} else {
(...some code)
}
}
func ReadData() ([]string) {
db, _ := sql.Open("mysql", "user1@/my_db")
(...some code)
rows, err := db.Query("select tweet from posts where username = ?", AddUser.Name) //AddUser is defined somewhere
(...some code)
for rows.Next() {
err := rows.Scan(&tweet)
if err != nil {
fmt.Println(err)
}
v := append(tweetarray, tweet)
fmt.Println(v)
return v
}
return []string{}
}
The html portion in the Go code:
const homePage = `
<html>
<h1>hi {{ .userName}}</h1>
<form action="/home/tweets" method="POST">
<label for="name">Tweet</label>
<input type="text" id="tweet" name="twt"</input>
<button type="Tweet">Tweet</button>
<h2>{{range $i := .tweetarray}} {{ $i }} {{end}}</h2>
`
The HTML doesn't appear at all. What am I doing wrong in the code?