从 HTTP 服务器中的文件中删除 .html 扩展名
许多 HTTP 服务器会自动在 URL 末尾添加“.html”扩展名,在某些情况下这可能是不可取的。要在 Go HTTP 服务器中修改此行为,您可以使用 http.Dir 实现自定义 http.FileSystem。具体方法如下:
- 创建自定义文件系统:
<code class="go">type HTMLDir struct { d http.Dir }</code>
- 实现 Open 方法:
Open 方法决定如何打开文件。根据您的要求,您有多种选择:
始终附加“.html”扩展名:
<code class="go">func (d HTMLDir) Open(name string) (http.File, error) { return d.d.Open(name + ".html") }</code>
回退到“.html”扩展名:
<code class="go">func (d HTMLDir) Open(name string) (http.File, error) { // Try name as supplied f, err := d.d.Open(name) if os.IsNotExist(err) { // Not found, try with .html if f, err := d.d.Open(name + ".html"); err == nil { return f, nil } } return f, err }</code>
以“.html”扩展名和后备开始:
<code class="go">func (d HTMLDir) Open(name string) (http.File, error) { // Try name with added extension f, err := d.d.Open(name + ".html") if os.IsNotExist(err) { // Not found, try again with name as supplied. if f, err := d.d.Open(name); err == nil { return f, nil } } return f, err }</code>
- 使用您的自定义文件系统:
<code class="go">fs := http.FileServer(HTMLDir{http.Dir("public/")}) http.Handle("/", http.StripPrefix("/", fs))</code>
通过实现 http.FileSystem 并自定义 Open 方法,您可以控制 HTTP 服务器如何提供文件,包括围绕“.html”扩展名的行为。
以上是如何从 Go HTTP 服务器中的 URL 中删除 .html 扩展名?的详细内容。更多信息请关注PHP中文网其他相关文章!

whentestinggocodewithinitfunctions,useexplicitseTupfunctionsorseParateTestFileSteSteTepteTementDippedDependendendencyOnInItfunctionsIdeFunctionSideFunctionsEffect.1)useexplicitsetupfunctionStocontrolglobalvaribalization.2)createSepEpontrolglobalvarialization

go'serrorhandlingurturnserrorsasvalues,与Javaandpythonwhichuseexceptions.1)go'smethodensursexplitirorhanderling,propertingrobustcodebutincreasingverbosity.2)

AnefactiveInterfaceoisminimal,clear and promotesloosecoupling.1)minimizeTheInterfaceForflexibility andeaseofimplementation.2)useInterInterfaceForeabStractionTosWapImplementations withCallingCallingCode.3)

集中式错误处理在Go语言中可以提升代码的可读性和可维护性。其实现方式和优势包括:1.将错误处理逻辑从业务逻辑中分离,简化代码。2.通过集中处理错误,确保错误处理的一致性。3.使用defer和recover来捕获和处理panic,增强程序健壮性。

Ingo,替代词Inivuntionsionializatializatializationfunctionsandsingletons.1)customInitializationfunctions hallowexpliticpliticpliticconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconcontirization curssementializatizatupsetups.2)单次固定元素限制ininconinconcurrent

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

Go语言的错误处理通过errors.Is和errors.As函数变得更加灵活和可读。1.errors.Is用于检查错误是否与指定错误相同,适用于错误链的处理。2.errors.As不仅能检查错误类型,还能将错误转换为具体类型,方便提取错误信息。使用这些函数可以简化错误处理逻辑,但需注意错误链的正确传递和避免过度依赖以防代码复杂化。

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境