Gopls:内联提示

内联提示是编辑器可以在源代码中内嵌显示的有用注解,例如函数调用中的参数名称。本文档描述了 gopls 提供的内联提示。

assignVariableTypes

"assignVariableTypes" 控制赋值语句中变量类型的内联提示

    i/* int*/, j/* int*/ := 0, len(r)-1

默认禁用。通过设置 "hints": {"assignVariableTypes": true} 来启用它。

compositeLiteralFields

"compositeLiteralFields" 复合字面量字段名的内联提示

    {/*in: */"Hello, world", /*want: */"dlrow ,olleH"}

默认禁用。通过设置 "hints": {"compositeLiteralFields": true} 来启用它。

compositeLiteralTypes

"compositeLiteralTypes" 控制复合字面量类型的内联提示

    for _, c := range []struct {
        in, want string
    }{
        /*struct{ in string; want string }*/{"Hello, world", "dlrow ,olleH"},
    }

默认禁用。通过设置 "hints": {"compositeLiteralTypes": true} 来启用它。

constantValues

"constantValues" 控制常量值的内联提示

    const (
        KindNone   Kind = iota/* = 0*/
        KindPrint/*  = 1*/
        KindPrintf/* = 2*/
        KindErrorf/* = 3*/
    )

默认禁用。通过设置 "hints": {"constantValues": true} 来启用它。

functionTypeParameters

"functionTypeParameters" 泛型函数上隐式类型参数的内联提示

    myFoo/*[int, string]*/(1, "hello")

默认禁用。通过设置 "hints": {"functionTypeParameters": true} 来启用它。

ignoredError

"ignoredError" 隐式丢弃错误的内联提示

    f.Close() // ignore error

此检查会在任何函数调用其错误结果被隐式忽略的语句后插入一个 // ignore error 提示。

要隐藏提示,请在调用语句后写一个实际包含“ignore error”的注释,或者显式将结果赋值给一个空白变量。少数常用函数,例如 fmt.Println,不包含在此检查范围内。

默认禁用。通过设置 "hints": {"ignoredError": true} 来启用它。

parameterNames

"parameterNames" 控制参数名的内联提示

    parseInt(/* str: */ "123", /* radix: */ 8)

默认禁用。通过设置 "hints": {"parameterNames": true} 来启用它。

rangeVariableTypes

"rangeVariableTypes" 控制 range 语句中变量类型的内联提示

    for k/* int*/, v/* string*/ := range []string{} {
        fmt.Println(k, v)
    }

默认禁用。通过设置 "hints": {"rangeVariableTypes": true} 来启用它。


本文档的源代码可以在 golang.org/x/tools/gopls/doc 下找到。