1-
1+ package pkg02_package_management
2+
3+ import (
4+ "fmt"
5+ "log"
6+ "strings"
7+
8+ "github.com/scagogogo/go-composer-sdk/pkg/composer"
9+ )
10+
11+ // Example04SearchPackage 演示如何使用Go Composer SDK搜索包
12+ func Example04SearchPackage () {
13+ fmt .Println ("\n === 搜索包示例 ===" )
14+
15+ // 创建一个Composer实例
16+ comp , err := composer .New (composer .DefaultOptions ())
17+ if err != nil {
18+ log .Fatalf ("初始化Composer失败: %v" , err )
19+ }
20+
21+ // 搜索关键字
22+ keyword := "logger"
23+ fmt .Printf ("搜索包: '%s'\n " , keyword )
24+
25+ // 执行搜索
26+ output , err := comp .Search (keyword )
27+ if err != nil {
28+ log .Fatalf ("搜索包失败: %v" , err )
29+ }
30+
31+ // 打印搜索结果
32+ fmt .Println ("搜索结果:" )
33+ fmt .Println (output )
34+
35+ // 示例:解析和处理搜索结果
36+ fmt .Println ("\n 提取搜索结果中的包名:" )
37+
38+ // 这只是一个简单的示例,实际解析可能需要更复杂的逻辑
39+ lines := strings .Split (output , "\n " )
40+ for _ , line := range lines {
41+ if strings .Contains (line , "/" ) && ! strings .HasPrefix (line , " " ) {
42+ parts := strings .Fields (line )
43+ if len (parts ) > 0 {
44+ fmt .Printf ("- %s\n " , parts [0 ])
45+ }
46+ }
47+ }
48+
49+ // 搜索另一个关键字(更具体的搜索)
50+ specificKeyword := "monolog"
51+ fmt .Printf ("\n 搜索特定包: '%s'\n " , specificKeyword )
52+
53+ specificOutput , err := comp .Search (specificKeyword )
54+ if err != nil {
55+ log .Fatalf ("搜索特定包失败: %v" , err )
56+ }
57+
58+ // 打印搜索结果
59+ fmt .Println ("搜索结果:" )
60+ fmt .Println (specificOutput )
61+ }
0 commit comments