Here is main.go
package main
import (
"fmt"
"net/http"
"html/template"
_ "github.com/mattn/go-sqlite3"
"database/sql"
)
type Page struct {
Name string
DBStatus bool
}
func main() {
templates := template.Must(template.ParseFiles("templates/index.html"))
db, _ := sql.Open("sqlite3", "dev.db")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
p := Page{Name: "Gopher", DBStatus: false}
if name := r.FormValue("name"); name != "" {
p.Name = name
}
p.DBStatus = db.Ping() == nil
if err := templates.ExecuteTemplate(w, "index.html", p); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
db.Close()
})
fmt.Println(http.ListenAndServe(":8080", nil))
}
and here is my index.html
<html>
<head></head>
<body>
<p>Hello, {{.Name}}</p>
{{if .DBStatus}}
<p>The database connection is alive!</p>
{{else}}
<p>The database connection is dead!</p>
{{end}}
</body>
</html>
When I run main.go I get the message
signal: killed
Process finished with exit code 1
I have install the go get github.com/mattn/go-sqlite3