gin项目对于favicon.ico请求的处理

Gin 是 Go语言写的一个 web 框架,它具有运行速度快,分组的路由器,良好的崩溃捕获和错误处理,非常好的支持中间件和 json

Gin开发的golang web项目,服务首页出现两次请求,其中一次是favicon.ico,我们需要适当的处理一下,不然favicon.ico的请求一直报404错误

go 复制代码
package main

import (
    "net/http"
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    r.Use(ginzap.Ginzap(logger, time.RFC3339, true))
	r.Use(ginzap.RecoveryWithZap(logger, true))
	r.StaticFile("/favicon.ico", "./static/favicon.ico")
	r.Static("/static", "./static")

    // Listen and serve on 0.0.0.0:80
    r.Run(":80")
}

这样前端首页在访问favicon.ico时就正常了

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <link rel="icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
{{.}}
</body>
</html>
相关推荐
nbsaas-boot9 小时前
Go vs Java 的三阶段切换路线图
java·开发语言·golang
modelmd12 小时前
Go 编程语言指南 练习题目分享
开发语言·学习·golang
福大大架构师每日一题16 小时前
2026年1月TIOBE编程语言排行榜,Go语言排名第16,Rust语言排名13。C# 当选 2025 年度编程语言。
golang·rust·c#
拔剑纵狂歌19 小时前
helm-cli安装资源时序报错问题问题
后端·docker·云原生·容器·golang·kubernetes·腾讯云
bing.shao21 小时前
AI在电商上架图片领域的应用
开发语言·人工智能·golang
源代码•宸21 小时前
Leetcode—712. 两个字符串的最小ASCII删除和【中等】
开发语言·后端·算法·leetcode·职场和发展·golang·dp
源代码•宸21 小时前
Golang语法进阶(Context)
开发语言·后端·算法·golang·context·withvalue·withcancel
源代码•宸21 小时前
Golang语法进阶(Sync、Select)
开发语言·经验分享·后端·算法·golang·select·pool
IT=>小脑虎1 天前
Go语言零基础小白学习知识点【基础版详解】
开发语言·后端·学习·golang
源代码•宸1 天前
Golang语法进阶(并发概述、Goroutine、Channel)
服务器·开发语言·后端·算法·golang·channel·goroutine