クイックスタート
Goプロジェクトで最初の類似性解析を実行してみましょう。基本機能を実演するための簡単な例を使用します。
ステップ1: テストプロジェクトの準備
Section titled “ステップ1: テストプロジェクトの準備”重複コードを含む簡単なGoプロジェクトを作成します:
mkdir test-projectcd test-projectgo mod init test-projectduplicate.goファイルを作成:
package main
import "fmt"
func processUserDataV1(name string, age int) { if name == "" { fmt.Println("Name is required") return } if age < 0 { fmt.Println("Age must be positive") return } fmt.Printf("Processing user: %s, age: %d\n", name, age)}
func processUserDataV2(username string, userAge int) { if username == "" { fmt.Println("Name is required") return } if userAge < 0 { fmt.Println("Age must be positive") return } fmt.Printf("Processing user: %s, age: %d\n", username, userAge)}
func main() { processUserDataV1("Alice", 25) processUserDataV2("Bob", 30)}ステップ2: 基本解析の実行
Section titled “ステップ2: 基本解析の実行”テストプロジェクトでsimilarity-goを実行:
./bin/similarity-go ./次のような出力が表示されるはずです:
🔍 Analyzing Go files in: ./📊 Found 2 similar functions with 85% similarity
Function Similarity Report==========================
📄 File: duplicate.go🔗 Similar Functions: • processUserDataV1 (lines 5-15) • processUserDataV2 (lines 17-27)
📈 Similarity Score: 85.7%📋 Differences: • Parameter names: name/username, age/userAge • Function names: processUserDataV1/processUserDataV2
💡 Suggestion: Consider refactoring to use a single functionステップ3: 類似性しきい値の調整
Section titled “ステップ3: 類似性しきい値の調整”類似性しきい値を変更して感度を調整できます:
# 90%以上の類似性のみを表示./bin/similarity-go --threshold 0.9 ./
# より多くの類似性を表示(70%以上)./bin/similarity-go --threshold 0.7 ./ステップ4: 出力形式
Section titled “ステップ4: 出力形式”similarity-goは複数の出力形式をサポートしています:
# プログラム処理用のJSON出力./bin/similarity-go --format json ./
# YAML出力./bin/similarity-go --format yaml ./
# 詳細な人間が読みやすい出力(デフォルト)./bin/similarity-go ./一般的な使用例
Section titled “一般的な使用例”特定のディレクトリの解析
Section titled “特定のディレクトリの解析”# srcディレクトリのみを解析./bin/similarity-go ./src
# 複数のディレクトリを解析./bin/similarity-go ./cmd ./internal ./pkgファイルとディレクトリの除外
Section titled “ファイルとディレクトリの除外”# カスタムワーカーと詳細出力を使用./bin/similarity-go --workers 4 --verbose ./CI/CDとの統合
Section titled “CI/CDとの統合”CIパイプラインに類似性チェックを追加:
# GitHub Actions例- name: Check code similarity run: | ./bin/similarity-go --threshold 0.8 --format json ./ > similarity-report.json if [ -s similarity-report.json ]; then echo "High similarity detected, consider refactoring" exit 1 fisimilarity-goはいくつかの重要な指標を提供します:
- 類似性スコア: 2つの関数がどの程度似ているかを示すパーセンテージ
- 行番号: 類似コードブロックの正確な位置
- 差異: 関数を異ならせる要素
- 提案: リファクタリングの推奨事項
設定ファイル
Section titled “設定ファイル”複雑なプロジェクトの場合、.similarity-go.yaml設定ファイルを作成します:
threshold: 0.8ignore: - "**/*_test.go" - "vendor/**" - "*.pb.go"output_format: "detailed"parallel: truecache_enabled: true現在のバージョンでは主にコマンドラインフラグを使用します:
./bin/similarity-go --threshold 0.8 --format json --workers 8 ./次のステップ
Section titled “次のステップ”基本を理解したので: