首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >golang源码分析 :gopls(7)

golang源码分析 :gopls(7)

作者头像
golangLeetcode
发布2026-03-18 18:23:55
发布2026-03-18 18:23:55
640
举报

研究完mainCommands 的Serve命令后,我们看下剩下的其他命令 首先是version,用来输出版本信息

代码语言:javascript
复制
func (v *version) Run(ctx context.Context, args ...string) error {
    var mode = debug.PlainText
    if v.JSON {
        mode = debug.JSON
    }
    return debug.PrintVersionInfo(ctx, os.Stdout, v.app.verbose(), mode)
}

接着是bug,如果环境变量TEST_GOPLS_BUG不为空,就会返回gopls的bug信息,接着就是对bug信息进行排序后输出

代码语言:javascript
复制
func (b *bug) Run(ctx context.Context, args ...string) error {
    // This undocumented environment variable allows
    // the cmd integration test (and maintainers) to
    // trigger a call to bug.Report.
    if msg := os.Getenv("TEST_GOPLS_BUG"); msg != "" {
        filecache.Start() // register bug handler
        goplsbug.Report(msg)
        return nil
    }
    // Enumerate bug reports, grouped and sorted.
    _, reports := filecache.BugReports()
    sort.Slice(reports, func(i, j int) bool {
        x, y := reports[i], reports[i]
        if x.Key != y.Key {
            return x.Key < y.Key // ascending key order
        }
        return y.AtTime.Before(x.AtTime) // most recent first
    })
    keyDenom := make(map[string]int) // key is "file:line"
    for _, report := range reports {
        keyDenom[report.Key]++
    }
    // Privacy: the content of 'public' will be posted to GitHub
    // to populate an issue textarea. Even though the user must
    // submit the form to share the information with the world,
    // merely populating the form causes us to share the
    // information with GitHub itself.
    //
    // For that reason, we cannot write private information to
    // public, such as bug reports, which may quote source code.
    public := &bytes.Buffer{}
    fmt.Fprint(public, goplsBugHeader)
    if len(reports) > 0 {
        fmt.Fprintf(public, "#### Internal errors\n\n")
        fmt.Fprintf(public, "Gopls detected %d internal errors, %d distinct:\n",
            len(reports), len(keyDenom))
        for key, denom := range keyDenom {
            fmt.Fprintf(public, "- %s (%d)\n", key, denom)
        }
        fmt.Fprintf(public, "\nPlease copy the full information printed by `gopls bug` here, if you are comfortable sharing it.\n\n")
    }
    debug.PrintVersionInfo(ctx, public, true, debug.Markdown)
    body := public.String()
    title := strings.Join(args, " ")
    if !strings.HasPrefix(title, goplsBugPrefix) {
        title = goplsBugPrefix + title
    }
    if !browser.Open("https://github.com/golang/go/issues/new?title=" + url.QueryEscape(title) + "&body=" + url.QueryEscape(body)) {
        fmt.Print("Please file a new issue at golang.org/issue/new using this template:\n\n")
        fmt.Print(body)
    }
    // Print bug reports to stdout (not GitHub).
    keyNum := make(map[string]int)
    for _, report := range reports {
        fmt.Printf("-- %v -- \n", report.AtTime)
        // Append seq number (e.g. " (1/2)") for repeated keys.
        var seq string
        if denom := keyDenom[report.Key]; denom > 1 {
            keyNum[report.Key]++
            seq = fmt.Sprintf(" (%d/%d)", keyNum[report.Key], denom)
        }
        // Privacy:
        // - File and Stack may contain the name of the user that built gopls.
        // - Description may contain names of the user's packages/files/symbols.
        fmt.Printf("%s:%d: %s%s\n\n", report.File, report.Line, report.Description, seq)
        fmt.Printf("%s\n\n", report.Stack)
    }
    if len(reports) > 0 {
        fmt.Printf("Please copy the above information into the GitHub issue, if you are comfortable sharing it.\n")
    }
    return nil
}

help会输出帮助信息,如果传了子命令名称,会输出子命令的帮助信息

代码语言:javascript
复制
func (h *help) Run(ctx context.Context, args ...string) error {
    find := func(cmds []tool.Application, name string) tool.Application {
        for _, cmd := range cmds {
            if cmd.Name() == name {
                return cmd
            }
        }
        return nil
    }
    // Find the subcommand denoted by args (empty => h.app).
    var cmd tool.Application = h.app
    for i, arg := range args {
        cmd = find(getSubcommands(cmd), arg)
        if cmd == nil {
            return tool.CommandLineErrorf(
                "no such subcommand: %s", strings.Join(args[:i+1], " "))
        }
    }
    // 'gopls help cmd subcmd' is equivalent to 'gopls cmd subcmd -h'.
    // The flag package prints the usage information (defined by tool.Run)
    // when it sees the -h flag.
    fs := flag.NewFlagSet(cmd.Name(), flag.ExitOnError)
    return tool.Run(ctx, fs, h.app, append(args[:len(args):len(args)], "-h"))
}

apiJSON会把文档信息以json的格式输出

代码语言:javascript
复制
func (j *apiJSON) Run(ctx context.Context, args ...string) error {
    os.Stdout.WriteString(doc.JSON)
    fmt.Println()
    return nil
}

licenses会输出license信息

代码语言:javascript
复制
func (l *licenses) Run(ctx context.Context, args ...string) error {
    txt := licensePreamble
    if licensespkg.Text == "" {
        txt += "(development gopls, license information not available)"
    } else {
        txt += licensespkg.Text
    }
    fmt.Fprint(os.Stdout, txt)
    return nil
}

整体来说,除了serve命令其他命令都是一些辅助的信息。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-06-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 golang算法架构leetcode技术php 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档