GO – 方法重复声明错误

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

GO - method redeclared error

问题

规则是,方法只能在命名类型和指向命名类型的指针上定义。


对于下面的代码

package main

type Cat struct {
}

func (c Cat) foo() {
   // do stuff_
}

func (c *Cat) foo() {
  // do stuff_
}

func main() {
	
}

编译器报错:

main.go:10: method redeclared: Cat.foo
	method(Cat) func()
	method(*Cat) func()

上述代码定义了:

命名类型(Cat)的方法 foo()

指向命名类型(*Cat)的方法 foo()

问题:

对于GO编译器来说,为什么为不同类型定义的方法被认为是相同的?

英文:

Rule is, methods can be defined only on named type and pointer to named type.


For the below code,

package main

type Cat struct {
}

func (c Cat) foo() {
   // do stuff_
}

func (c *Cat) foo() {
  // do stuff_
}

func main() {
	
}

compiler gives error:

main.go:10: method redeclared: Cat.foo
	method(Cat) func()
	method(*Cat) func()

Above code defines,

method foo() for named type(Cat) and

method foo() for pointer to named type(*Cat).

Question:

For GO compiler, Why methods defined for different type is considered
same?

答案1

得分: 4

在Go语言中,接收器是一种语法糖。函数(c Cat) foo()的实际运行时签名是foo(c Cat)。接收器被移动到第一个参数。

Go语言不支持名称重载。在一个包中只能有一个名为foo的函数。

根据上述陈述,你可以看到会有两个具有不同签名的名为foo的函数。这种语言不支持这样做。

在Go语言中你不能这样做。一个经验法则是为指针接收器编写一个方法,Go语言将在你有指针或值时使用它。

如果你仍然需要两个变体,你需要给方法命名不同的名称。

英文:

In Go, receivers are a kind of syntactic sugar. The actual, runtime signature of function (c Cat) foo() is foo(c Cat). The receiver is moved to a first parameer.

Go does not support name overloading. There can be only one function of with name foo in a package.

Having said the statements above, you see that there would be two functions named foo with different signatures. This language does not support it.

You cannot do that in Go. The rule of thumb is to write a method for a pointer receiver and Go will use it whenever you have a pointer or value.

If you still need two variants, you need name the methods differently.

答案2

得分: -2

例如,您可以像这样模拟一些猫的行为:

package main

import (
	"fmt"
)

type Growler interface{
	Growl() bool
}

type Cat struct{
    Name string
    Age int
} 

// *Cat 适用于对象和“对象引用”(指向对象的指针)
func (c *Cat) Speak() bool{
	fmt.Println("喵喵!")
	return true
}

func (c *Cat) Growl() bool{
	fmt.Println("咕噜!")
	return true
}


func main() {
	var felix Cat // 不是指针
	felix.Speak() // 可以工作 :-)
	felix.Growl() // 可以工作 :-)

	var ginger *Cat = new(Cat) 
	ginger.Speak() // 可以工作 :-)
	ginger.Growl() // 可以工作 :-)
}

以上是给定代码的中文翻译。

英文:

For example, you can model some feline behavior like this:

package main

import (
    "fmt"
)

type Growler interface{
    Growl() bool
}

type Cat struct{
    Name string
    Age int
} 

// *Cat is good for both objects and "object references" (pointers to objects)
func (c *Cat) Speak() bool{
    fmt.Println("Meow!")
    return true
}

func (c *Cat) Growl() bool{
    fmt.Println("Grrr!")
    return true
}


func main() {
    var felix Cat // is not a pointer
    felix.Speak() // works :-)
    felix.Growl() // works :-)

    var ginger *Cat = new(Cat) 
    ginger.Speak() // works :-)
    ginger.Growl() // works :-)
}

huangapple
  • 本文由 发表于 2017年1月30日 06:55:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/41926900.html
匿名

发表评论

匿名网友

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

确定