快捷菜单
常用功能一站直达
更多功能请点顶栏「快捷菜单」
自己再it pc上推送 hugo-teek项目后,测试都是没问题的,可以正常运行网站。
来到work pc上,拉取仓库后,本地运行项目后,报错如下:

126752@One MINGW64 /d/hugo-teek (master)
2$ hugo server --source=hugo-teek-site --bind=0.0.0.0 --port=8080 --buildDrafts
3Watching for changes in D:\hugo-teek\hugo-teek-site\{assets,content,data,static,themes}
4Watching for config changes in D:\hugo-teek\hugo-teek-site\hugo.toml
5Start building sites …
6hugo v0.148.2-40c3d8233d4b123eff74725e5766fc6272f0a84d+extended+withdeploy windows/amd64 BuildDate=2025-07-27T12:43:24Z VendorInfo=gohugoio
7
8ERROR render of "/" failed: "D:\hugo-teek\hugo-teek-site\themes\hugo-teek\layouts\index.html:217:13": execute of template failed: template: index.html:217:13: executing "main" at <partial "site-info-card.html" .>: error calling partial: "D:\hugo-teek\hugo-teek-site\themes\hugo-teek\layouts\partials\site-info-card.html:3:18": execute of template failed: template: _partials/site-info-card.html:3:18: executing "_partials/site-info-card.html" at <len $docAnalysis.fileList>: error calling len: reflect: call of reflect.Value.Type on zero Value
9Built in 620 ms
10Error: error building site: render: failed to render pages: render of "/" failed: "D:\hugo-teek\hugo-teek-site\themes\hugo-teek\layouts\index.html:217:13": execute of template failed: template: index.html:217:13: executing "main" at <partial "site-info-card.html" .>: error calling partial: "D:\hugo-teek\hugo-teek-site\themes\hugo-teek\layouts\partials\site-info-card.html:3:18": execute of template failed: template: _partials/site-info-card.html:3:18: executing "_partials/site-info-card.html" at <len $docAnalysis.fileList>: error calling len: reflect: call of reflect.Value.Type on zero Value
11
1226752@One MINGW64 /d/hugo-teek (master)根据错误信息,核心问题是 $docAnalysis 变量未初始化或为 nil,导致访问 $docAnalysis.fileList 时触发空值错误。以下是分步解决方案:
错误来自主题的 partial 模板:
1themes/hugo-teek/layouts/partials/site-info-card.html打开上述文件,找到第3行(出错行),原代码应该类似:
1{{ len $docAnalysis.fileList }}由于 $docAnalysis 可能未定义或为 nil,直接访问 fileList 会报错。需要添加双重空值判断:
1{{ if and $docAnalysis $docAnalysis.fileList }}{{ len $docAnalysis.fileList }}{{ else }}0{{ end }}1{{ $fileCount := 0 }}
2{{ if $docAnalysis }}
3 {{ if $docAnalysis.fileList }}
4 {{ $fileCount = len $docAnalysis.fileList }}
5 {{ end }}
6{{ end }}
7{{ $fileCount }}$docAnalysis 正确定义(可选)如果 $docAnalysis 是你自己定义的变量,需要在使用前确保它被初始化(避免 nil):
1{{/* 在模板开头添加变量初始化 */}}
2{{ $docAnalysis := .Site.Data.docAnalysis | default (dict "fileList" nil) }}docAnalysis 是数据文件(data/docAnalysis.yaml),会自动加载{fileList: nil},避免空指针修改后重新启动 Hugo 服务器:
1hugo server --source=hugo-teek-site --bind=0.0.0.0 --port=8080 --buildDrafts如果没有再出现 reflect: call of reflect.Value.Type on zero Value 错误,说明修复成功。
Hugo 模板中访问嵌套字段时,必须确保每一层都非空:
$docAnalysis.fileList,但 $docAnalysis 可能未定义(nil)len 函数无法接收 nil 值,导致反射错误and $docAnalysis $docAnalysis.fileList 先判断两层非空,再调用 len 即可避免错误后面不知道为啥就正常了。。。。🤣🤣🤣
日历 · 精选 · 友链 · 更多
如果内容对你有帮助,欢迎请我喝杯咖啡 ☕



One的公众号
爱折腾博客的小白