英文:
Strange method output
问题
我对我的简单程序的输出感到困惑。我期望在输出中得到所有四个名字,但是我无法在输出中得到第一个名字。请帮助我解决这个问题,或者提供一些相关资源。
type Employees struct {
Name string
}
func main() {
chandran := Employees{Name: "Chandran"}
darpan := Employees{Name: "Darpan"}
ishaan := Employees{Name: "Ishaan"}
manbir := Employees{Name: "Manbir"}
Employees.structName(chandran, darpan, ishaan, manbir)
}
func (e Employees) structName(names ...Employees){
fmt.Println(names)
}
英文:
I am confused about the output from my simple program. I expect to get all four names in the output, but I cannot get the first name in the output. Please help me clear this, or some resource about that.
type Employees struct {
Name string
}
func main() {
chandran := Employees{Name: "Chandran"}
darpan := Employees{Name: "Darpan"}
ishaan := Employees{Name: "Ishaan"}
manbir := Employees{Name: "Manbir"}
Employees.structName(chandran, darpan, ishaan, manbir)
}
func (e Employees) structName(names ...Employees){
fmt.Println(names)
}
答案1
得分: 5
在Go语言中,没有"static"方法。要调用一个方法,你必须拥有一个接收者类型的值,并在该值上调用方法。
所以你必须这样调用:
var e Employees
e.structName(chandran, darpan, ishaan, manbir)
或者简单地:
Employees{}.structName(chandran, darpan, ishaan, manbir)
这两种方式的输出结果如下(在Go Playground上尝试一下):
[{Chandran} {Darpan} {Ishaan} {Manbir}]
为什么你的版本省略了第一个参数?
因为你实际上有一个方法表达式,你正在调用该方法表达式。
让我们详细看一下:
Employees.structName
是一个方法表达式,它是一个可调用的函数。它的签名是structName
方法的签名,参数列表前缀是接收者。所以Employees.structName
的类型是:
func(main.Employees, ...main.Employees)
你通过传递4个员工来调用它,其中第一个将作为接收者,只有剩下的3个将作为names
参数使用。因此,只有从第二个开始的名称将被打印出来。
英文:
There are no "static" methods in Go. To call a method, you must have a value of the receiver type, and call the method on that value.
So you must call it like this:
var e Employees
e.structName(chandran, darpan, ishaan, manbir)
Or simply:
Employees{}.structName(chandran, darpan, ishaan, manbir)
Both these output (try it on the Go Playground):
[{Chandran} {Darpan} {Ishaan} {Manbir}]
Why does your version omit the first argument?
Because what you have is in fact a method expression, and you are calling that method expression.
Let's look at it in details:
Employees.structName
is a method expression, and it is a callable function. Its signature is the signature of the structName
method, the parameter list prefixed with the receiver. So the type of Employees.structName
is:
func(main.Employees, ...main.Employees)
You call this by passing 4 employees, first of which will be the receiver, and only the remaining 3 will be used as the names
argument. So only the names starting from the second will be printed.
答案2
得分: 3
根据语言规范:
https://go.dev/ref/spec#Method_expressions
表达式
Employees.structName(chandran, darpan, ishaan, manbir)
等同于:
chandran.strucName(darpan,ishaan,manbir)
这是因为函数Employees.structName
的第一个参数被解释为方法的接收器。
如果你这样写:
Employees{}.structName(chandran, darpan, ishaan, manbir)
它将打印出所有四个元素。
英文:
According to the language specification:
https://go.dev/ref/spec#Method_expressions
The expression
Employees.structName(chandran, darpan, ishaan, manbir)
is equivalent to:
chandran.strucName(darpan,ishaan,manbir)
This is because the first argument to the function Employees.structName
is interpreted as the receiver to the method.
If you do:
Employees{}.structName(chandran, darpan, ishaan, manbir)
it will print all four elements.
答案3
得分: 1
我认为你应该将你的结构体命名为"Employee"而不是"Employees",因为它只代表一个员工。如果你想在一组(切片)员工上执行方法,可以这样定义:
type Employee struct {
Name string
}
type Employees struct {
employees []Employee
}
然后在Employees类型上定义函数:
func (e *Employees) Add(names ...Employee) {
e.employees = append(e.employees, names...)
}
func (e Employees) structName() {
fmt.Println(e.employees)
}
我在Go Playground上创建了一个演示,其中包含Add和StructName函数。
英文:
I think you should name your Strut Employee instead of Employees. since it represents only one employee. If you want to have methods on a group(slice) of employees.
type Employee struct {
Name string
}
type Employees struct {
employees []Employee
}
then define function on that group type Employees:
func (e *Employees) Add(names ...Employee) {
e.employees = append(e.employees, names...)
}
func (e Employees) structName() {
fmt.Println(e.employees)
}
I made a Demo on Go Playground with an Add and StructName function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论