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 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000