main.gohandler.gomain.gohandler.Home
handler.goHome()main.goHome()

main.go

package main
 
import (
    "fmt"
    "net/http"

    "github.com/ibilalkayy/WEBAPP1/handler"
)

func main() {
    http.HandleFunc("/", handler.Home)
    fmt.Println("Starting the server at :8080")
    http.ListenAndServe(":8080", nil)
}

handler.go

package handler

import (
    "html/template"
    "net/http"
)
var tmpl = make(map[string]*template.Template)

func init() {
    tmpl["home"] = template.Must(template.ParseFiles("templates/home.html", "templates/base.html"))
}

func Home(w http.ResponseWriter, r *http.Request) {
    tmpl["home"].ExecuteTemplate(w, "home.html", nil)
}

main.go仅限版本

package main

import (
    "fmt"
    "html/template"
    "net/http"
)

var tmpl = make(map[string]*template.Template)

func init() {
    tmpl["home"] = template.Must(template.ParseFiles("templates/home.html", "templates/base.html"))
}

func home(w http.ResponseWriter, r *http.Request) {
    tmpl["home"].ExecuteTemplate(w, "home.html", nil)
}

func main() {
    http.HandleFunc("/", home)
    fmt.Println("Starting the server at :8080")
    http.ListenAndServe(":8080", nil)
}