I'm trying to show my result set and Variables from .Go Lang to an HTML Page.
Assuming this tree view as my workspace. Home.HTML
<div id="Golang"><p>My Name is : <i>{{.myName}}</i></p></div>
<div id="Golang"><p>Some Random Database Test: <br>{{.Country}} {{.City}} </p></div>
db.Go
package main
import (
"database/sql"
"fmt"
"html/template"
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
)
type world struct {
Country sql.NullString `json:"country"`
State sql.NullString `json:"state"`
City sql.NullString `json:"city"`
Abbr string `json:"abbriviation"`
id int `json:"id"`
CCode int `json:"CountryCode"`
}
func main() {
name := "Sam"
dsn := "root:1234@tcp(127.0.0.1:3306)/script"
// Open database
fmt.Println("We're going to connect a MySql Db")
db, err := sql.Open("mysql", dsn)
if err != nil {
panic(err)
}
defer db.Close()
fmt.Println("Successfully connected to Mysql Db")
results, err := db.Query("SELECT * FROM CountryDb WHERE Country =?", "India")
if err != nil {
panic(err.Error())
}
for results.Next() {
var tag world
err = results.Scan(&tag.Abbr, &tag.Country, &tag.CCode, &tag.State, &tag.City)
if err != nil {
panic(err.Error())
}
log.Printf(tag.Abbr, tag.Country, tag.CCode, tag.State, tag.City)
}
}
Now how can i show values from my Go program to HTML Tags. This is my first go program and I'm not much aware of this language. I go through some online tutorials but the way they are written is not very helpful. so any help on this would be appreciated. Thanks