当前位置:网站首页>Golang:go静态文件处理
Golang:go静态文件处理
2022-08-01 06:50:00 【ZzzWClock】
go静态文件处理
一.前言
golang在没有处理静态文件前是访问不到的,必须用到net/http包下的
http.Handle(),http.StripPrefix(),http.FileServer(),http.Dir()
二.示例
- http.StripPrefix 会截取输入的前缀,http.FileServer 会自动在目录里面寻找文件,配合http.Dir使用
package main
import (
"fmt"
"html/template"
"net/http"
)
// html处理器
func htmlHandle(w http.ResponseWriter, r *http.Request) {
// 解析一个模版文件
// Must函数用于包装返回(*Template, error)的函数/方法调用,它会在err非nil时panic,一般用于变量初始化:
// ParseFiles函数创建一个模板并解析filenames指定的文件里的模板定义。返回的模板的名字是第一个文件的文件名(不含扩展名),内容为解析后的第一个文件的内容。至少要提供一个文件。如果发生错误,会停止解析并返回nil。
t := template.Must(template.ParseFiles("index.html"))
// Execute方法将解析好的模板应用到data上,并将输出写入wr。如果执行时出现错误,会停止执行,但有可能已经写入wr部分数据。模板可以安全的并发执行。
t.Execute(w, "我是被传递的一段话")
}
func main() {
// 路由器
http.HandleFunc("/", handle)
http.HandleFunc("/html", htmlHandle)
// 静态文件处理器
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
// 开启server服务
http.ListenAndServe(":8081", nil)
}
<!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>Document</title>
<link rel="stylesheet" href="/static/css/layui.css">
<script src="/static/js/layui.js"></script>
</head>
<body>
<p style="color: red;">我是一个模版文件:{
{.}}</p>
<div>
<img src="/static/image/docker.png" alt="图片" style="width: 200px;">
</div>
</body>
</html>
- 访问正常
边栏推荐
- 从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
- 【音视频】srs直播平台搭建
- CSP-S2019兴奋不已
- R语言使用tidyquant包的tq_transmute函数计算持有某只股票的天、月、周收益率、ggplot2使用条形图可视化股票月收益率数据、使用百分比显示Y轴坐标数据、使用不同的色彩表征正负收益率
- [Translation] Securing cloud-native communications: From ingress to service mesh and beyond
- Introduction to the basic principles, implementation and problem solving of crawler
- LeetCode 0150. 逆波兰表达式求值
- Practical training Navicat Chinese and English mode switching
- 权重等比分配
- 【一句话攻略】彻底理解JS中的回调(Callback)函数
猜你喜欢
Guest brush SQL - 2
从零开始—仿牛客网讨论社区项目(一)
特别数的和
点餐系统数据库设计--SQL Server
MVVM项目开发(商品管理系统一)
从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?
curl (7) Failed connect to localhost8080; Connection refused
Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?
rhcsa 第四天
随机推荐
I have three degrees, and I have five faces. I was "confessed" by the interviewer, and I got an offer of 33*15.
Datagrip error "The specified database userpassword combination is rejected..."Solutions
How JS works
MVVM project development (commodity management system 1)
小白的0基础教程SQL: 安装MYSQL 03
图像基本操作的其他内容
Qt Widget project loading example of qml
湖仓一体电商项目(一):项目背景和架构介绍
爬虫框架 Scrapy 详解
仿牛客网项目总结
uva12326
我三本学历,五面阿里,被面试官“供”着出来了,拿了33*15的Offer
Hunan institute of technology in 2022 ACM training sixth week antithesis
Guest brush SQL - 2
ORACLE modify another user package (package)
从购买服务器到网站搭建成功保姆级教程~超详细
Jupyter shortcuts
【FiddlerScript】利用FiddlerScript抓包保利威下载
问下 mysql向pg同步多个表的话 有什么好的方案吗?
【南瓜书ML】(task4)神经网络中的数学推导(更新ing)