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

如何在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 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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog