go-function-analysis
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGo Function Analysis
Go函数分析
Overview
概述
This skill analyzes all Go functions in a workspace and generates:
- A complete list of functions with their line counts and maximum call depths
- Statistical analysis (p50, p90, p99 percentiles)
该技能会分析工作区内的所有Go函数,并生成:
- 包含函数行数和最大调用深度的完整函数列表
- 统计分析结果(p50、p90、p99分位数)
When to Use
使用场景
Use this skill when:
- Auditing code complexity in a Go project
- Identifying long functions that may need refactoring
- Generating code metrics for documentation
- Reviewing function size distribution
在以下场景中使用该技能:
- 审计Go项目中的代码复杂度
- 识别可能需要重构的过长函数
- 为文档生成代码指标
- 查看函数大小分布情况
How to Execute
执行步骤
Step 1: Find All Go Files
步骤1:查找所有Go文件
Find all files in the workspace, excluding common dependency directories:
.gobash
find . -name "*.go" -type f ! -path "./vendor/*" ! -path "./.git/*" ! -path "*_test.go"查找工作区内所有文件,排除常见的依赖目录:
.gobash
find . -name "*.go" -type f ! -path "./vendor/*" ! -path "./.git/*" ! -path "*_test.go"Step 2: Extract Functions and Calculate Lengths
步骤2:提取函数并计算长度
For each file, use the helper script to extract function information:
.gobash
./scripts/analyze_functions.sh <workspace_path>Or manually parse using this approach for each file:
- Find all function declarations with line numbers
- For each function, count lines from declaration to matching closing brace
func - Record: file path, function name, start line, end line, total lines
对于每个文件,使用辅助脚本提取函数信息:
.gobash
./scripts/analyze_functions.sh <workspace_path>或者手动为每个文件按以下方式解析:
- 查找所有带行号的函数声明
- 对于每个函数,统计从声明到匹配的闭合大括号的行数
func - 记录:文件路径、函数名、起始行、结束行、总行数
Step 3: Calculate Percentiles
步骤3:计算分位数
Given all function lengths sorted ascending:
- p50 (median): Value at position
count * 0.50 - p90: Value at position
count * 0.90 - p99: Value at position
count * 0.99
将所有函数长度按升序排序后:
- p50(中位数):位于位置的值
count * 0.50 - p90:位于位置的值
count * 0.90 - p99:位于位置的值
count * 0.99
Step 4: Generate README.function.md
步骤4:生成README.function.md
Create in the workspace root with this format:
README.function.mdmarkdown
undefined在工作区根目录创建,格式如下:
README.function.mdmarkdown
undefinedGo Function Analysis
Go Function Analysis
Generated: YYYY-MM-DD
Generated: YYYY-MM-DD
Summary Statistics
Summary Statistics
| File Path | Function Name | Function Lines | Depth Level | Percentile |
|---|---|---|---|---|
| path/to/f | funcName | X | Y | p50 |
| ... | ... | ... | ... | ... |
Total functions: N
Average length: X lines
| File Path | Function Name | Function Lines | Depth Level | Percentile |
|---|---|---|---|---|
| path/to/f | funcName | X | Y | p50 |
| ... | ... | ... | ... | ... |
Total functions: N
Average length: X lines
All Functions (sorted by length, descending)
All Functions (sorted by length, descending)
| File Path | Function Name | Function Lines | Depth Level |
|---|---|---|---|
| path/to/f | funcName | X | Y |
| ... | ... | ... | ... |
---| File Path | Function Name | Function Lines | Depth Level |
|---|---|---|---|
| path/to/f | funcName | X | Y |
| ... | ... | ... | ... |
---Manual Analysis Approach
手动分析方法
If the script is not available, follow these steps:
如果脚本不可用,请遵循以下步骤:
Finding Functions in Go
在Go中查找函数
Go functions are declared with:
go
func FunctionName(params) returnType {
// body
}
func (receiver Type) MethodName(params) returnType {
// body
}Go函数的声明格式为:
go
func FunctionName(params) returnType {
// body
}
func (receiver Type) MethodName(params) returnType {
// body
}Counting Lines
统计行数
For each function:
- Start from the keyword line
func - Count until the matching closing
} - Include the and
funclines in the count}
对于每个函数:
- 从关键字所在行开始
func - 统计到匹配的闭合所在行
} - 统计时包含行和
func行}
Example
示例
go
func example() { // Line 1
fmt.Println() // Line 2
if true { // Line 3
doSomething()// Line 4
} // Line 5
} // Line 6This function has 6 lines.
go
func example() { // Line 1
fmt.Println() // Line 2
if true { // Line 3
doSomething()// Line 4
} // Line 5
} // Line 6该函数共有6行。
Important Notes
重要说明
[!NOTE] Only analyzefiles within the workspace. Exclude.go,/vendor/, and external dependencies./.git/
[!TIP] Functions over 50 lines may be candidates for refactoring. p90+ functions often warrant closer review.
[!NOTE] 仅分析工作区内的文件。 排除.go、/vendor/和外部依赖项。/.git/
[!TIP] 超过50行的函数可能是重构的候选对象。 p90及以上的函数通常需要更仔细的审查。