名称为空的方法的目的是什么?

What is purpose of methods with blank names?

刚开始学习golang,在阅读规范的过程中发现了一些自己无法解决的问题。在关于方法声明的部分中,语言规范说 "If the base type is a struct type, the non-blank method and field names must be distinct."

https://golang.org/ref/spec#Method_declarations

据我了解,名称为空的方法是

func (t T) _() {
  // some cool code
}

那么,我该如何使用它,这些方法的主要目的是什么?

空白方法名没有任何实际意义,您不能以任何方式调用它们(即使通过反射也不行,它们不会出现在该类型的(导出的)方法中,请参阅Go Playground)。只是语言规范没有明确禁止。

方法名称是:

MethodName     = identifier .

方法名可以是任何有效的 identifier:

identifier     = letter { letter | unicode_digit } .
letter         = unicode_letter | "_" .
unicode_letter = /* a Unicode code point classified as "Letter" */ .
unicode_digit  = /* a Unicode code point classified as "Number, decimal digit" */ .

短语 "the non-blank method and field names must be distinct" 只是表示方法(和字段)名称必须不同,但您可以添加 2 个单独的空白方法,它们不会冲突。空白方法的名称不会与任何内容发生冲突,包括其他空白方法。