go-function-analysis

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Go Function Analysis

Go函数分析

Overview

概述

This skill analyzes all Go functions in a workspace and generates:
  1. A complete list of functions with their line counts and maximum call depths
  2. Statistical analysis (p50, p90, p99 percentiles)
该技能会分析工作区内的所有Go函数,并生成:
  1. 包含函数行数和最大调用深度的完整函数列表
  2. 统计分析结果(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
.go
files in the workspace, excluding common dependency directories:
bash
find . -name "*.go" -type f ! -path "./vendor/*" ! -path "./.git/*" ! -path "*_test.go"
查找工作区内所有
.go
文件,排除常见的依赖目录:
bash
find . -name "*.go" -type f ! -path "./vendor/*" ! -path "./.git/*" ! -path "*_test.go"

Step 2: Extract Functions and Calculate Lengths

步骤2:提取函数并计算长度

For each
.go
file, use the helper script to extract function information:
bash
./scripts/analyze_functions.sh <workspace_path>
Or manually parse using this approach for each file:
  1. Find all function declarations with line numbers
  2. For each function, count lines from
    func
    declaration to matching closing brace
  3. Record: file path, function name, start line, end line, total lines
对于每个
.go
文件,使用辅助脚本提取函数信息:
bash
./scripts/analyze_functions.sh <workspace_path>
或者手动为每个文件按以下方式解析:
  1. 查找所有带行号的函数声明
  2. 对于每个函数,统计从
    func
    声明到匹配的闭合大括号的行数
  3. 记录:文件路径、函数名、起始行、结束行、总行数

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
README.function.md
in the workspace root with this format:
markdown
undefined
在工作区根目录创建
README.function.md
,格式如下:
markdown
undefined

Go Function Analysis

Go Function Analysis

Generated: YYYY-MM-DD
Generated: YYYY-MM-DD

Summary Statistics

Summary Statistics

File PathFunction NameFunction LinesDepth LevelPercentile
path/to/ffuncNameXYp50
...............
Total functions: N Average length: X lines
File PathFunction NameFunction LinesDepth LevelPercentile
path/to/ffuncNameXYp50
...............
Total functions: N Average length: X lines

All Functions (sorted by length, descending)

All Functions (sorted by length, descending)

File PathFunction NameFunction LinesDepth Level
path/to/ffuncNameXY
............

---
File PathFunction NameFunction LinesDepth Level
path/to/ffuncNameXY
............

---

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:
  1. Start from the
    func
    keyword line
  2. Count until the matching closing
    }
  3. Include the
    func
    and
    }
    lines in the count
对于每个函数:
  1. func
    关键字所在行开始
  2. 统计到匹配的闭合
    }
    所在行
  3. 统计时包含
    func
    行和
    }

Example

示例

go
func example() {     // Line 1
    fmt.Println()    // Line 2
    if true {        // Line 3
        doSomething()// Line 4
    }                // Line 5
}                    // Line 6
This 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 analyze
.go
files within the workspace. Exclude
/vendor/
,
/.git/
, and external dependencies.
[!TIP] Functions over 50 lines may be candidates for refactoring. p90+ functions often warrant closer review.
[!NOTE] 仅分析工作区内的
.go
文件。 排除
/vendor/
/.git/
和外部依赖项。
[!TIP] 超过50行的函数可能是重构的候选对象。 p90及以上的函数通常需要更仔细的审查。