Golang serve html files tutorial # 3

Golang serve html files tutorial # 3

we need two files.

  1. main.go file
  2. HTML file

to serve html files we use the following code:

tmpl, _ := template.ParseFiles(“index.html”)

 

<div>
<div>package main</div>
<div>import (</div>
<div>    "fmt"</div>
<div>    "html/template"</div>
<div>    "net/http"</div>
<div>)</div>
<div>func main() {</div>
<div>    tmpl, _ := template.ParseFiles("index.html")</div>
<div>    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {</div>
<div>        tmpl.Execute(w, nil)</div>
<div>    })</div>
<div>    http.ListenAndServe(":8080", nil)</div>
<div>}</div>
</div>

HTML FILE

<div>
<div>&lt;html lang="en"&gt;</div>
<div>&lt;head&gt;</div>
<div>    &lt;meta charset="UTF-8"&gt;</div>
<div>    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;</div>
<div>    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;</div>
<div>    &lt;title&gt;GOLANG HTML&lt;/title&gt;</div>
<div>&lt;/head&gt;</div>
<div>&lt;body&gt;</div>
<div>    HTML PAGE</div>
<div>    &lt;h1&gt;HTML PAGE&lt;/h1&gt;</div>
<div>    &lt;h2&gt;HTML PAGE&lt;/h2&gt;</div>
<div>    &lt;h3&gt;HTML PAGE&lt;/h3&gt;</div>
<div>&lt;/body&gt;</div>
<div>&lt;/html&gt;</div>
</div>

 

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: