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条)

报告相同问题?

悬赏问题

  • ¥40 求一款能支持ios15以上的屏蔽越狱插件。比较好用的
  • ¥15 C++ QT对比内存字符(形式不定)
  • ¥30 C++第三方库libiconv 远程安装协助
  • ¥15 https://github.com/youlaitech/vue3-element-admin/blob/master/src/store/modules/user.ts 这2句代码如何理解
  • ¥15 duilib开发文本字串超过长度显示
  • ¥20 mysql的.ibd文件为啥那么多
  • ¥15 C++中采用栈和BFS算法求解迷宫问题
  • ¥15 关于#java#的问题:这是跳转失败出现的界面这是哪个出现问题的servlet(开发工具-ide)
  • ¥15 EBS R12费用采购跨月冲销
  • ¥15 python中用mplfinance如何做到多股同列?