登录
首页 >  Golang >  Go问答

使用golang来运行html和css文件

来源:stackoverflow

时间:2024-03-03 10:54:29 367浏览 收藏

从现在开始,努力学习吧!本文《使用golang来运行html和css文件》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

大家,我在通过golang文件将css包含在html文件中时遇到问题。 本地服务器输出只有html文件没有css,请问如何解决?

也许我使用模板包的方式有问题,所以你能解释一下如何以不同的方式进行路由吗?示例:当您访问http://localhost:8080/login时,它会显示login.html。我看到了有关它的 net/http 文档,但要么我是盲目的,要么我只是试图在那里找到错误的东西。 所有文件都在同一目录中

welcome.html





website
    



    

enter

registration

样式.css

@charset "utf-8";
/* css document */
body
{
    font-family: "comic sans ms";
    background-image: url(images/bg.jpg);
    background-repeat: repeat ;
    background-size: 80px 80px ;
}
h1
{
margin: 0;
text-transform: uppercase;
padding-bottom: 5px;
border-bottom: 3px solid rgba(58,87,15,0.80);
}
form 
{
    margin : 0 auto;
    background: rgba(123,170,52,0.76);
    width: 450px;
    height: 350px;
    padding: 20px;
    box-shadow: 2px 2px 5px rgba(0,0,0,0.82);
}
.group
{
    margin: 16px ;
    padding: 5px;

}
label
{
    padding-left: 10px;
    text-transform: uppercase;
}
input
{
    margin-top: 5px;
    height: 30px;
    width: 400px;
    border-radius:20px/20px;
    border: none;
    padding-left: 15px;
    font-size: 18px;
    box-shadow: 2px 2px 5px rgba(0,0,0,0.82);
}

input:focus{
    border: 2px solid #264503;
    transform: translatex(15px);
    width: 385px;
}
button{
    font-family: "comic sans ms";
    cursor: pointer;
    padding: 10px 20px;
    height: 40px;
    color:aliceblue;
    background: rgba(21,73,3,1.00);
    border: none;
    text-transform: uppercase;
    font-size: 15px;
    box-shadow: 2px 2px 5px rgba(0,0,0,0.82);
}
button:hover{
    font-weight: bold;
    transform: scale(1.1);
}

.link{
    font-family: "comic sans ms";
    cursor: pointer;
    padding: 10px 20px;
    height: 40px;
    color:aliceblue;
    background: rgba(21,73,3,1.00);
    border: none;
    text-transform: uppercase;
    font-size: 15px;
    box-shadow: 2px 2px 5px rgba(0,0,0,0.82);
    text-decoration: none; 

}

gofile.go

package main

import (
    "fmt"
    "html/template"
    "net/http"
)

func welcome(w http.ResponseWriter, r *http.Request) {

    tmpl := template.Must(template.ParseFiles("welcome.html"))

    tmpl.Execute(w, nil)
}

func login(w http.ResponseWriter, r *http.Request) {
    tmpl := template.Must(template.ParseFiles("login.html"))

    tmpl.Execute(w, nil)
}

func main() {
    http.HandleFunc("/", welcome)
    http.HandleFunc("/login", login)

    fmt.Println("Listening...")
    http.ListenAndServe(":8080", nil)
}

**输出如下:**

总结:如何使用golang net/http或html/template包显示带有css的页面? 如何正确进行页面之间的路由?抱歉有错误。预先感谢各位!


解决方案


你的 go 服务器不知道它应该提供 style.css 因为你从未告诉过它。如果您将该文件移至 assets/ 子目录,则可以注册一个处理程序来为该目录提供服务:

http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))

另请参阅 this answer

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《使用golang来运行html和css文件》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>