How to use type alias in interface

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

How to use type alias in interface

问题

考虑以下程序:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type MyMethod func(i int, j int, k int) (string, error)
  6. type MyInterface interface {
  7. Hello MyMethod
  8. }
  9. type myMethodImpl struct{}
  10. func (*myMethodImpl) Hello(i int, j int, k int) (string, error) {
  11. return fmt.Sprintf("%d:%d:%d\n", i, j, k), nil
  12. }
  13. func main() {
  14. var im MyInterface = &myMethodImpl{}
  15. im.Hello(0, 1, 2)
  16. }

如何在接口声明中使用MyMethod而不是重复方法签名?

英文:

Consider the following program:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type MyMethod func(i int, j int, k int) (string, error)
  6. type MyInterface interface {
  7. Hello(i int, j int, k int) (string, error)
  8. }
  9. type myMethodImpl struct {}
  10. func (*myMethodImpl) Hello(i int, j int, k int) (string, error) {
  11. return fmt.Sprintf("%d:%d:%d\n", i, j, k), nil
  12. }
  13. func main() {
  14. var im MyInterface = &myMethodImpl{}
  15. im.Hello(0, 1, 2)
  16. }

How do I use MyMethod in the interface declaration instead of repeating the method signature?

答案1

得分: 3

你在这里混淆了两个不同的概念。一个是你定义了一个函数类型的类型。当你想要在另一个类型中使用这个函数类型时,你需要使用结构体。

将你的代码更改为可能的解决方案1(不太符合Go的习惯):

  1. type MyMethod func(i int, j int, k int) (string, error)
  2. type myMethodImpl struct {
  3. Hello MyMethod
  4. }
  5. var hello MyMethod = func(i int, j int, k int) (string, error) {
  6. return fmt.Sprintf("%d:%d:%d\n", i, j, k), nil
  7. }
  8. func main() {
  9. im := &myMethodImpl{Hello: hello}
  10. fmt.Println(im.Hello(0, 1, 2))
  11. }

另一个解决方案是将其改为使用接口。这个解决方案就是你的代码,只是没有定义MyMethod类型。这里是示例代码。

但是它们之间有什么区别呢?

如果你将一个函数定义为类型,那么在创建函数时就需要声明它。

  1. var hello MyMethod = func(i int, j int, k int) (string, error) {
  2. return fmt.Sprintf("%d:%d:%d\n", i, j, k), nil
  3. }

现在,hello的类型就是MyMethod。如果有另外一个类型,例如:

  1. type YourMethod func(i int, j int, k int) (string, error)

hello仍然只是MyMethod类型。

要定义一个接口,你需要一个方法集。但是MyMethod类型不是一个方法,它只是一个函数类型。所以函数类型和方法定义是不同的概念。对于Go来说,如果你想要将一个字符串定义为方法,它也是一样的。

英文:

You are mixing two different things here. The one is that you define a type which is a function. When you want that type inside another type you need to use a struct.

Changing your code to a possible solution 1 (not so idiomatic go):

  1. type MyMethod func(i int, j int, k int) (string, error)
  2. type myMethodImpl struct {
  3. Hello MyMethod
  4. }
  5. var hello MyMethod = func(i int, j int, k int) (string, error) {
  6. return fmt.Sprintf("%d:%d:%d\n", i, j, k), nil
  7. }
  8. func main() {
  9. im := &myMethodImpl{Hello: hello}
  10. fmt.Println(im.Hello(0, 1, 2))
  11. }

https://play.golang.org/p/MH-WOnj-Mu

The other solution would be to change it to use an interface. That solution is your code just without the type definition of MyMethod. https://play.golang.org/p/nGctnTSwnC

But what is the difference between?

If you define a func as a type you need to declare it, when you create a function.

  1. var hello MyMethod = func(i int, j int, k int) (string, error) {
  2. return fmt.Sprintf("%d:%d:%d\n", i, j, k), nil
  3. }

Now hello has just exactly the type MyMethod. If there would be another type for example:

  1. type YourMethod func(i int, j int, k int) (string, error)

hello will still just the type MyMethod.

To define an interface you need a method set. But the type MyMethod is not a method. It is just a function type. So the function type is something different then a method definition. For go it would be the same if you want to define a string as a method.

huangapple
  • 本文由 发表于 2017年3月11日 13:36:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/42731762.html
匿名

发表评论

匿名网友

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

确定