douzhong3038
douzhong3038
2019-07-25 00:23
浏览 400
已采纳

如何在Golang中使用HTML

I am learning golang and trying to make a simple website. This is my folder structure.

- ui
   |
   - login.html
- cmd
   |
   - main.go

My main.go

package main

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

var tmpl *template.Template

func init() {
    tmpl = template.Must(template.ParseFiles("../ui/login.html"))
}

func main() {
    http.HandleFunc("/", foo)
    http.ListenAndServe(":8080", nil)
}

func foo(reswt http.ResponseWriter, req *http.Request) {
    tmpl.ExecuteTemplate(reswt, "../ui/login.html", nil)

}

login.html

<html>
    <form method="POST">
        <label for="uname">User Name</label>
        <input type="text" id="uname" name="username">
        <br>

        <input type="submit">

    </form>
</html>

When I executre main.go, I do not get errors. But nothing on localhost:8080.

If I keep main.go and login.html same folder, this works.

Why this folder structure is not working? I have tried [this SO thread], but this is not solving my problem1

Following parts added after css is not working.

<style>
input[type=submit]:active {
  background: #cde5ef;
  border-color: #9eb9c2 #b3c0c8 #b4ccce;
  -webkit-box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2);
  box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2);
}

</style>


<div class="login">
  <h1>Login to Web App</h1>
  <form method="post" action="">
    <p><input type="text" name="login" value="" placeholder="Username or Email"></p>
    <p><input type="password" name="password" value="" placeholder="Password"></p>
    <p class="remember_me">
      <label>
        <input type="checkbox" name="remember_me" id="remember_me">
        Remember me on this computer
      </label>
    </p>
    <p class="submit"><input type="submit" name="commit" value="Login"></p>
  </form>
</div>

<div class="login-help">
  <p>Forgot your password? <a href="#">Click here to reset it</a>.</p>
</div>

When I go to localhost:8080

I get following output in my browser.

<style> input[type=submit]:active {   background: #cde5ef;   border-color: #9eb9c2 #b3c0c8 #b4ccce;   -webkit-box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2);   box-shadow: inset 0 0 3px rgba(0, 0, 0,
0.2); } } </style>


<div class="login">   <h1>Login to Web App</h1>   <form method="post" action="">
    <p><input type="text" name="login" value="" placeholder="Username or Email"></p>
    <p><input type="password" name="password" value="" placeholder="Password"></p>
    <p class="remember_me">
      <label>
        <input type="checkbox" name="remember_me" id="remember_me">
        Remember me on this computer
      </label>
    </p>
    <p class="submit"><input type="submit" name="commit" value="Login"></p>   </form> </div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhang1926
    douzhang1926 2019-07-25 00:42
    已采纳

    ParseFiles stores the names of the list of files as template name. That means, in your case, login.html should be used in the execution, while ../ui/login.html is unavailable.


    This would work, as login.html has already been named by init().

    func init() {
        tmpl = template.Must(template.ParseFiles("../ui/login.html"))
    }
    
    func foo(reswt http.ResponseWriter, req *http.Request) {
        tmpl.ExecuteTemplate(reswt, "login.html", nil)
    }
    
    点赞 评论
  • dounuo9921
    dounuo9921 2019-07-25 06:43

    You can Call like this for Generic type

    import(
    "html/template"
    )
    
    // output html
    func OutputHTML(w http.ResponseWriter, filename string, data interface{}) {
       t, err := template.ParseFiles(filename)
       if err != nil {
           http.Error(w, err.Error(), 500)
           return
       }
       if err := t.Execute(w, data); err != nil {
           http.Error(w, err.Error(), 500)
           return
       }
    }
    

    And you call this like

    OutputHTML(w, "anyhtmlfile.html",nil)
    
    点赞 评论

相关推荐