【问题标题】:How to use html with golang如何在 golang 中使用 html
【发布时间】:2019-07-25 00:23:55
【问题描述】:

我正在学习 golang 并尝试制作一个简单的网站。这是我的文件夹结构。

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

我的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>

当我执行main.go 时,我没有收到错误。但是localhost:8080 上什么都没有。

如果我将 main.gologin.html 保留在同一个文件夹中,则可以。

为什么这个文件夹结构不起作用?我试过[this SO thread],但这并不能解决我的问题1

css 之后添加的以下部分不起作用。

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

当我去localhost:8080

我在浏览器中得到以下输出。

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

【问题讨论】:

    标签: html templates go


    【解决方案1】:

    ParseFiles 将文件列表的名称存储为模板名称。这意味着,在您的情况下,应该在执行中使用login.html,而../ui/login.html 不可用。


    这会起作用,因为 login.html 已经被 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)
    }
    

    【讨论】:

    • 你能详细说明一下怎么做吗?请详细解释..因为我是golang初学者。
    【解决方案2】:

    你可以像这样调用通用类型

    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
       }
    }
    

    你这样称呼它

    OutputHTML(w, "anyhtmlfile.html",nil)
    

    【讨论】:

    • 感谢您的一般回答。我还有另一个问题,当我发布html 时,样式没有被解析。所有内容都打印为一行html 页。
    • 无法理解您的问题,请在此处添加示例代码以获得更好的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2022-01-20
    • 2019-02-11
    • 2020-07-18
    • 2021-06-13
    相关资源
    最近更新 更多