How to use type alias in interface

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

How to use type alias in interface

问题

考虑以下程序:

package main

import (
	"fmt"
)

type MyMethod func(i int, j int, k int) (string, error)

type MyInterface interface {
	Hello MyMethod
}

type myMethodImpl struct{}

func (*myMethodImpl) Hello(i int, j int, k int) (string, error) {
	return fmt.Sprintf("%d:%d:%d\n", i, j, k), nil
}

func main() {
	var im MyInterface = &myMethodImpl{}
	im.Hello(0, 1, 2)
}

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

英文:

Consider the following program:

package main

import (
	"fmt"
)

type MyMethod func(i int, j int, k int) (string, error)

type MyInterface interface {
  Hello(i int, j int, k int) (string, error)
}

type myMethodImpl struct {}

func (*myMethodImpl) Hello(i int, j int, k int) (string, error) {
   return fmt.Sprintf("%d:%d:%d\n", i, j, k), nil
}

func main() {
	var im MyInterface = &myMethodImpl{}
	im.Hello(0, 1, 2)
}

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

答案1

得分: 3

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

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

type MyMethod func(i int, j int, k int) (string, error)

type myMethodImpl struct {
    Hello MyMethod
}

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

func main() {
    im := &myMethodImpl{Hello: hello}
    fmt.Println(im.Hello(0, 1, 2))
}

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

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

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

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

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

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):

type MyMethod func(i int, j int, k int) (string, error)

type myMethodImpl struct {
	Hello MyMethod
}

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

func main() {
	im := &myMethodImpl{Hello: hello}
	fmt.Println(im.Hello(0, 1, 2))
}

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.

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

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

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:

确定