应用启动后是否可以重新加载html模板?

Right now I currently parse all the templates into a variable like so:

var templates = template.New("Template")

func LoadTemplates() {
    filepath.Walk("view/", func(path string, info os.FileInfo, err error) error {
        if strings.HasSuffix(path, ".html") {
            templates.ParseFiles(path)
        }
        return nil
    })
}

func Render(w http.ResponseWriter, tmplName string, data interface{}) {
    if err := templates.ExecuteTemplate(w, tmplName, data); err != nil {
        log.Println(err)
    }
}

So if I make any changes, I need to restart the entire app. Is there any way I can make it so that the changes are reflected when they're made

Yes, it is possible to reload (html or text) templates at runtime.

As long as the folder that you are reading the templates from is accessible by the application you can simply repeat the template parsing.

You could do something like this:

var templates = template.New("Template")

func folderChanged(folder string) bool {
    // todo: implement filesystem watcher here
    return true
}

func ReloadTemplates(templateFolder string) {

    newTemplates, err := LoadTemplates(templateFolder)
    if err != nil {
        log.Println("Unable to load templates: %s", err)
        return
    }

    // override existing templates variable
    templates = newTemplates
}

func LoadTemplates(folder string) (*template.Template, error) {
    template := template.New("Template")

    walkError := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
        if strings.HasSuffix(path, ".html") {
            _, parseError := template.ParseFiles(path)
            if parseError != nil {
                return parseError
            }

        }

        return nil

    })

    return template, walkError
}

func Render(w http.ResponseWriter, tmplName string, data interface{}) {
    templateFolder := "view"
    if folderChanged(templateFolder) {
        ReloadTemplates(templateFolder)
    }

    if err := templates.ExecuteTemplate(w, tmplName, data); err != nil {
        log.Println(err)
    }

}

If you are using a relative path like the ./view-folder from your example it will only work if the directory from which you are executing the application has such a sub-folder. If that is not the case I would suggest using a path relative to the users' home directory or something like that.

It is completly OK to reload your templates on every request when developing / in debug mode.

You can define an interface and two "template executors" like so

type TemplateExecutor interface{
    ExecuteTemplate(wr io.Writer, name string, data interface{}) error
}

type DebugTemplateExecutor struct  {
    Glob string
}

func (e DebugTemplateExecutor) ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
    t := template.Must(template.ParseGlob(e.Glob))
    return t.ExecuteTemplate(wr, name, data)
}

type ReleaseTemplateExecutor struct  {
    Template *template.Template
}

func (e ReleaseTemplateExecutor) ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
    return e.Template.ExecuteTemplate(wr, name, data)
}

that wrap template.Execute(). Pick which one you want at runtime e.g.

const templateGlob = "templates/*.html"
const debug = true

var executor TemplateExecutor

func main() {
    if debug {
        executor = DebugTemplateExecutor{templateGlob}

    } else {
        executor = ReleaseTemplateExecutor{
            template.Must(template.ParseGlob(templateGlob)),
        }
    }

    http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
        executor.ExecuteTemplate(w, "test.html", nil)
    })

    http.ListenAndServe(":3000", nil)
}

If you need hot reload in production, i'd suggest watching the directory for changes instead of compiling it on every request.