【发布时间】: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.go 和 login.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>
【问题讨论】: