GoLang 中为什么使用 ioutil.ReadFile() 读取文件时,无需指定 HTML 中链接的文件?
golang学习网今天将给大家带来《GoLang 中为什么使用 ioutil.ReadFile() 读取文件时,无需指定 HTML 中链接的文件?》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!
我有一个 golang 脚本,旨在根据浏览器中的输入查询动态构建网页,如下所示 http://localhost:8000/blog/post#
,使用的是 post#
部分确定要解析到我创建的 html 模板中的 json 数据文件;例如,如果我输入 http://localhost:8000/blog/post1
那么它会从我的 post1.json
文件创建一个 index.html
文件。目前,我的脚本在运行时允许我在浏览器中加载单个页面,然后退出并在终端标准输出日志中显示错误:
2022/03/18 00:32:02 error: open jsofun.css.json: no such file or directory exit status 1
这是我当前的脚本:
package main import ( "encoding/json" "html/template" "io/ioutil" "log" "net/http" "os" ) func bloghandler(w http.responsewriter, r *http.request) { blogstr := r.url.path[len("/blog/"):] blogstr = blogstr + ".json" // read the file in locally json_file, err := ioutil.readfile(blogstr) if err != nil { log.fatal("error: ", err) } // define a data structure type blogpost struct { // in order to see these elements later, these fields must be exported // this means capitalized naming and the json field identified at the end title string `json:"title"` timestamp string `json:"timestamp"` main string `json:"main"` contentinfo string `json:"content_info"` } // json data var obj blogpost err = json.unmarshal(json_file, &obj) if err != nil { log.fatal("error: ", err) } tmpl, err := template.parsefiles("./blogtemplate.html") htmlfile, err := os.create("index.html") if err != nil { log.fatal(err) } defer htmlfile.close() tmpl.execute(htmlfile, obj) http.servefile(w, r, "./index.html") } func main() { http.handlefunc("/blog/", bloghandler) log.fatal(http.listenandserve(":8080", nil)) }
我做了一些基本的调试,发现问题出在以下几行:
json_file, err := ioutil.readfile(blogstr) if err != nil { log.fatal("error: ", err) }
令我困惑的是为什么 ioutil.readfile 也尝试读取我的 html 中链接的文件?浏览器不应该处理该链接而不是我的处理程序吗?作为参考,这是我的 html,其中链接了文件 jsofun.css
;控制台输出中引用的错误显示我的脚本正在尝试在调用 ioutil.readfile
期间以 jsofun.css.json
的形式访问此文件:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>dynamic json events</title> <link rel="stylesheet" href="./jsofun.css"></style> </head> <body> <section id="title"> <h1 id="text-title">{{.title}}</h1> <time id="timestamp"> {{.timestamp}} </time> </section> <nav role="navigation" id="site-nav"> <ul id="sitemap"> </ul> </nav> <main role="main" id="main"> {{.main}} </main> <footer role="contentinfo" id="footer"> <section id="content-info" role="contentinfo"> {{.contentinfo}} </section> <form id="contact-form" role="form"> <address> contact me by <a id="my-email" href="mailto:[email protected]" class="my-email">e-mail</a> </address> </form> </footer> <script src="./jsofun.js"> </script> </body> </html>
我知道我已将 .json
附加到以下行中的查询字符串:blogstr = blogstr + ".json"
,但是,我不明白为什么这也会影响我的 html 中的链接。我是否遗漏了什么或者为什么它会读取我的 html 模板中的链接?我想要 ioutil.readfile
做的就是读取解析到我的模板中的 json 文件。
编辑:我想补充一点,当我最初将页面加载到浏览器中时,它会成功加载 json 中的文本内容,但没有任何样式。当我查看源代码时,链接也是正确的,这让我很困惑为什么样式表不适用,因为它位于可以访问的正确目录中。这是我在浏览器中选择查看源时的源:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>dynamic json events</title> <link rel="stylesheet" href="./jsofun.css"></style> </head> <body> <section id="title"> <h1 id="text-title">second post test</h1> <time id="timestamp"> friday, march 18th, 12:21am </time> </section> <nav role="navigation" id="site-nav"> <ul id="sitemap"> </ul> </nav> <main role="main" id="main"> this is my second post where i am able to use dynamic url routing </main> <footer role="contentinfo" id="footer"> <section id="content-info" role="contentinfo"> this post was used primarily to test and layout how the rest of my posts will appear. </section> <form id="contact-form" role="form"> <address> contact me by <a id="my-email" href="mailto:[email protected]" class="my-email">e-mail</a> </address> </form> </footer> <script src="./jsofun.js"> </script> </body> </html>
作为参考,这是我的 json 文件的示例:
{ "title" : "Second Post Test", "timestamp": "Friday, March 18th, 12:21AM", "main": "This is my second post where I am able to use dynamic URL routing", "content_info": "This post was used primarily to test and layout how the rest of my posts will appear." }
正确答案
您的 go 服务器设置为仅为 /blog/
路径提供服务,它通过执行 bloghandler
来实现这一点。您的 go 服务器中没有任何其他内容被设置为提供 css、js 或图像文件等资源。
对于此类事情,您通常需要在单独的路径注册单独的 FileServer
处理程序。示例:
func main() { http.handlefunc("/blog/", bloghandler) // to serve a directory on disk (/path/to/assets/on/my/computer) // under an alternate url path (/assets/), use stripprefix to // modify the request url's path before the fileserver sees it: http.handle("/assets/", http.stripprefix("/assets/", http.fileserver(http.dir("/path/to/assets/on/my/computer")))) log.fatal(http.listenandserve(":8080", nil)) }
您需要修复的另一件事是 html 中这些资产字段的链接,它们应该是绝对的,而不是相对的。
... <link rel="stylesheet" href="/assets/jsofun.css"></style> ... <script src="/assets/jsofun.js">
当然,仅当资产位于 /path/to/assets/on/my/computer
目录中时,上述内容才有效,例如
/path/to/assets/on/my/computer ├── jsofun.css └── jsofun.js
您 bloghandler
不必要地为每个请求创建一个新文件而不删除它,这有可能很快将您的磁盘填满到其最大容量。要提供模板,您不需要创建新文件,您可以直接在 http.resposewriter
中执行模板。还建议仅解析模板一次,尤其是在生产代码中,以避免不必要的资源浪费:
type BlogPost struct { Title string `json:"title"` Timestamp string `json:"timestamp"` Main string `json:"main"` ContentInfo string `json:"content_info"` } var blogTemplate = template.Must(template.ParseFiles("./blogtemplate.html")) func blogHandler(w http.ResponseWriter, r *http.Request) { blogstr := r.URL.Path[len("/blog/"):] + ".json" f, err := os.Open(blogstr) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } defer f.Close() var post BlogPost if err := json.NewDecoder(f).Decode(&post); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := blogTemplate.Execute(w, post); err != nil { log.Println(err) } }
让我们看看当您请求 http://localhost:8000/blog/post#
时会发生什么。
浏览器请求页面;您的代码成功构建并返回一些 html
- 这将包括:
浏览器接收并处理html;作为该过程的一部分,它请求上面的 css
。现在原始请求位于文件夹 /blog/post#
中,因此 ./jsofun.css
变为 http://localhost:8000/blog/jsofun.css
。
当你的go应用程序收到这个请求时,bloghandler
将会被调用(由于请求路径);它删除 /blog/
然后添加 .json
以获得文件名 jsofun.css.json
。然后,您尝试打开此文件并收到错误,因为它不存在。
有几种方法可以解决这个问题;更改模板以使用 可能是一个开始(但我不知道
jsofun.css
存储在哪里,并且您没有显示任何可以使用的代码该文件)。我认为还值得注意的是,您不必在磁盘上创建文件 index.html
(除非有其他原因这样做)。
(有关其他问题和进一步步骤,请参阅 mkopriva 的答案 - 当该答案发布时,我已经输入了一半,并且感觉该演练可能仍然有用)。
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

- 上一篇
- 解决sqlmock数据库关闭的问题

- 下一篇
- 避免写几十行的方法:如何更有效地处理具有 3 个不同指针类型的参数的 Sprintf?
-
- Golang · Go问答 | 1年前 |
- 在读取缓冲通道中的内容之前退出
- 139浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 戈兰岛的全球 GOPRIVATE 设置
- 204浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将结构作为参数传递给 xml-rpc
- 325浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何用golang获得小数点以下两位长度?
- 477浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何通过 client-go 和 golang 检索 Kubernetes 指标
- 486浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将多个“参数”映射到单个可变参数的习惯用法
- 439浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将 HTTP 响应正文写入文件后出现 EOF 错误
- 357浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 结构中映射的匿名列表的“复合文字中缺少类型”
- 352浏览 收藏
-
- Golang · Go问答 | 1年前 |
- NATS Jetstream 的性能
- 101浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将复杂的字符串输入转换为mapstring?
- 440浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 相当于GoLang中Java将Object作为方法参数传递
- 212浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何确保所有 goroutine 在没有 time.Sleep 的情况下终止?
- 143浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 茅茅虫AIGC检测
- 茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
- 58次使用
-
- 赛林匹克平台(Challympics)
- 探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
- 76次使用
-
- 笔格AIPPT
- SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
- 86次使用
-
- 稿定PPT
- 告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
- 79次使用
-
- Suno苏诺中文版
- 探索Suno苏诺中文版,一款颠覆传统音乐创作的AI平台。无需专业技能,轻松创作个性化音乐。智能词曲生成、风格迁移、海量音效,释放您的音乐灵感!
- 83次使用
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- Golang取得代码运行时间的问题
- 2023-02-24 501浏览
-
- 请问 go 代码如何实现在代码改动后不需要Ctrl+c,然后重新 go run *.go 文件?
- 2023-01-08 501浏览
-
- 如何从同一个 io.Reader 读取多次
- 2023-04-11 501浏览