在Go语言中,使用前导下划线(leading underscore)在变量名中的时机是什么?

huangapple go评论101阅读模式
英文:

When to use leading underscore in variable names in Go

问题

在变量名前面加上 _ 的特殊目的是什么?

例如:
func (_m *MockTracker)...

来自这里

英文:

Is there any special purpose of leading _ in a variable's name?

Example:
func (_m *MockTracker)...

from here.

答案1

得分: 11

在Go语言中,标识符名称中的前导下划线没有特殊的定义意义。根据Go语言规范中的定义,标识符是由一个或多个字母和数字组成的序列,而且第一个字符必须是字母。

你提供的示例代码是从mockgen.go文件中生成的代码。

在你提供的包中,你会看到类似以下的内容:

// Recorder for MockTracker (not exported)
type _MockTrackerRecorder struct {
    mock *MockTracker
}

mockgen包中的sanitize函数会在包名前面添加下划线,并且似乎是为了保持一致性和确保标识符名称保持私有(即不以大写字母开头而无法导出)。但这并不是Go语言规范中定义的内容。

下面是sanitize函数的代码:

// sanitize cleans up a string to make a suitable package name.
func sanitize(s string) string {
    t := ""
    for _, r := range s {
        if t == "" {
            if unicode.IsLetter(r) || r == '_' {
                t += string(r)
                continue
            }
        } else {
            if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
                t += string(r)
                continue
            }
        }
        t += "_"
    }
    if t == "_" {
        t = "x"
    }
    return t
}

更多信息请参考Go语言规范中的标识符部分mockgen.go文件

英文:

There is no special meaning defined for a leading underscore in an identifier name in the spec:

> Identifiers
>
> Identifiers name program entities such as variables and types. An
> identifier is a sequence of one or more letters and digits. The first
> character in an identifier must be a letter.
>
> identifier = letter { letter | unicode_digit } .
>
> a
> _x9
> ThisVariableIsExported
> αβ

Your sample is generated code from mockgen.go.

In the package you linked you'll see things like:

// Recorder for MockTracker (not exported)
type _MockTrackerRecorder struct {
    mock *MockTracker
}

The sanitize function in the mockgen package prepends an underscore to package names and it seems that it's otherwise used for consistency and to ensure that identifier names remain private (i.e. not exported because they start with a capital letter). But it's not something that is defined in the Go spec.

// sanitize cleans up a string to make a suitable package name.
func sanitize(s string) string {
	t := ""
	for _, r := range s {
		if t == "" {
			if unicode.IsLetter(r) || r == '_' {
				t += string(r)
				continue
			}
		} else {
			if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
				t += string(r)
				continue
			}
		}
		t += "_"
	}
	if t == "_" {
		t = "x"
	}
	return t
}

答案2

得分: 4

在变量名的命名约定中,似乎没有关于下划线(_)的内容。参考这里:effective go

英文:

It seems that there is nothing regarding the _ in a variable name in the naming convetions.
From here: effective go

答案3

得分: 3

另一个用例是用于未导出的全局变量。这是许多Go开发人员遵循的约定,并在Uber风格指南的此部分中有解释。

英文:

Another use case is for unexported global variables. It's a convention that many Go developers follow and explained in this section of the Uber style guide.

huangapple
  • 本文由 发表于 2015年4月27日 17:20:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/29891677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定