gpt4 book ai didi

html - 使用HandleFunc提供通过模板链接的CSS文件

转载 作者:行者123 更新时间:2023-12-01 20:25:50 25 4
gpt4 key购买 nike

我没有看到有关此问题的最新问题,我很困惑。截至目前,我正在使用HandleFunc和模板将文件提供给Google Cloud。我是应届毕业生,还没有使用过大量的Golang,但是我想在自己熟悉的同时熟悉它。使用其他指南,问题和视频,我已成功将html文件提供给服务器,并且能够显示标题。我正在使用存储在我的模板目录中的header.html模板,该模板链接到我的css目录中的main.css。然后,我在目前用作主要文件的html文件中使用标题模板。我无法获得要使用的CSS或图像。我的猜测是,我需要在main.go文件中编写更多内容。.我尝试使用Handle,Handler和HandleFunc以多种方式处理它,但我承认我对这些功能的工作原理和区别还是有些粗略的了解,但是我不明白是什么原因造成了我。
文件结构:

├── app.yaml
├── main.go
└── static
├── css
│   └── main.css
├── emailList.html
├── img
│   ├── TheBuzzTraders.png
│   ├── favicon.ico
│   └── tbtWordLogo.png
├── js
└── templates
└── header.html
main.go:
package main

import (
"fmt"
"html/template"
"net/http"
"os"
"path/filepath"
"strings"
)

func emailListHandler(w http.ResponseWriter, r *http.Request) {
tpl.ExecuteTemplate(w, "emailList", &Page{Title: "Welcome to my site"})
}

func main() {
http.HandleFunc("/", emailListHandler)
fmt.Println(http.ListenAndServe(":8080", nil))
}

var tpl = func() *template.Template {
t := template.New("")
err := filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
if strings.Contains(path, ".html") {
fmt.Println(path)
_, err = t.ParseFiles(path)
if err != nil {
fmt.Println(err)
}
}
return err
})

if err != nil {
panic(err)
}
return t
}()

type Page struct {
Title string
}
emailList.html:
{{define "emailList"}}
<!DOCTYPE html>
<html lang="en">
{{template "header" .}}
<body>
<h1>Invest in your tomorrow</h1>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
</body>
</html>
{{end}}
header.html:
{{define "header"}}
<head>
<!-- Bootstrap and CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="../css/main.css">

<meta charset="UTF-8">
<title>The Buzz Traders</title>
</head>
{{end}}
对不起所有代码,但我确实很困惑。如何获取CSS和图像文件以与emailList.html一起使用?先感谢您!!
与@Mayank解决方案类似,答案是:
    http.HandleFunc("/", emailListHandler)
fs := http.FileServer(http.Dir("static"))
http.Handle("/css/", fs)
http.Handle("/img/", fs)
http.Handle("/templates/", fs)
fmt.Println(http.ListenAndServe(":8080", nil))
}```

最佳答案

要提供静态文件,您应该使用http包的FileServer方法。尝试将主要功能代码更改为类似

func main() {
http.HandleFunc("/", emailListHandler)
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
fmt.Println(http.ListenAndServe(":8080", nil))
}

关于html - 使用HandleFunc提供通过模板链接的CSS文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63653146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com