2

how can I show the data data I created in go on the index html page? It puts an "." to the tutorial I watched, but the same code doesn't work for me. Can you help me?

 package main

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

func main() {

    http.HandleFunc("/", anaSayfa)
    http.ListenAndServe(":8080", nil)
}

func anaSayfa(w http.ResponseWriter, r *http.Request) {

    view, _ := template.ParseFiles("index.html")
    data := "Go'dan gelen veri"
    view.Execute(w, data)

}

At the bottom, I have the index.html page.

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Page Title</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel='stylesheet' type='text/css' media='screen' href='main.css'>
    <script src='main.js'></script>
</head>
<body>
    <p>Selam</p>
    {{ 
        . 

    }}
</body>
</html> 

this page is coming

Harun
  • 21
  • 2
  • Check errors (returned when parsing and executing the template). And you shouldn't parse your templates in the handler: [It takes too much time when using "template" package to generate a dynamic web page to client in Golang](https://stackoverflow.com/questions/28451675/it-takes-too-much-time-when-using-template-package-to-generate-a-dynamic-web-p/28453523#28453523) – icza Aug 03 '22 at 10:19
  • Your example also works for me. Not reproducible. – icza Aug 03 '22 at 10:20

0 Answers0