Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: Go 2: add a keyword to facilitate the implementation of the interface mechanism #35645

Closed
fuyao-w opened this issue Nov 17, 2019 · 6 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@fuyao-w
Copy link

fuyao-w commented Nov 17, 2019

This is the trouble I encountered at work.

在 go 中实现一个接口,需要自己找到并实现 interface 下面所有的函数,在代码量非常多的项目中,这么做非常不方便,为什么不像其他语言那样,用一个 impl 关键字将实现一个接口的所有函数聚合到一处 。就像下面的例子:

To implement an interface in go, you need to find and implement all the functions of the interface. In a project with a lot of code, this is very inconvenient. Why not use an impl keyword to generate all the functions of an interface together.Like the example below:

type (  
        Interface interface {
	     Len() int
	     Less(i, j int) bool
	     Swap(i, j int)
        }
	CountDownAction struct {
		Action  int    `json:"action"`
	}
)
var CountDownList []CountDownAction

CountDownList impl  Interface{
    func (receiver CountDownList) Len() int {
	    return len(receiver)
    }
    func (receiver CountDownList) Less(i, j int) bool {
	    return receiver[i].Action <= receiver[j].Action
    }

    func (receiver CountDownList) Swap(i, j int) {
	    receiver[i], receiver[j] = receiver[j], receiver[i]
    }

}

这样,IDE 就可以为我们省下不少的工作

In this way, the IDE can save a lot of work for us.

@fuyao-w fuyao-w changed the title proposal:Why not add a keyword to facilitate the implementation of the interface mechanism | 为什么不增加一个关键字用于方便的实现接口机制 proposal : interface type :Why not add a keyword to facilitate the implementation of the interface mechanism | 为什么不增加一个关键字用于方便的实现接口机制 Nov 17, 2019
@ianlancetaylor ianlancetaylor changed the title proposal : interface type :Why not add a keyword to facilitate the implementation of the interface mechanism | 为什么不增加一个关键字用于方便的实现接口机制 proposal: Go 2: add a keyword to facilitate the implementation of the interface mechanism Nov 17, 2019
@gopherbot gopherbot added this to the Proposal milestone Nov 17, 2019
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange and removed Proposal labels Nov 17, 2019
@ianlancetaylor
Copy link
Contributor

If I understand your example, it would work if you simply omitted the words CountDownList impl Interface. What is the advantage of adding them?

@fuyao-w
Copy link
Author

fuyao-w commented Nov 17, 2019

If I understand your example, it would work if you simply omitted the words CountDownList impl Interface. What is the advantage of adding them?

First of all, my colleague and I sometimes complain that it is difficult to find which interface the structure is implemented in go.

Moreover, a few days ago, when I needed to implement three interfaces in a reusable logic, I needed to jump to the location defined by the interface to copy the function signature every time. I think this is very inconvenient. If each interface needs to implement 10 functions, then I will crash directly. It was a very good idea, but it was very painful to implement. I have to do a lot of copying and jumping operations.

and so

  1. This can be just a syntactic sugar, the goal is to reduce unnecessary work through IDE shortcuts.When using IDE encoding, the IDE can also generate code for us directly via shortcut keys. But if I code directly in the txt file, or the IDE directly supports implementing interface shortcuts. It doesn't matter.

  2. I think this can make the code structure clearer.

首先,我和我的同事有时抱怨在 go 里面找那个结构体实现了哪个接口很困难。
而且,前几天当我在一个可复用的逻辑里面需要实现三个接口,但是我每次都需要跳到接口定义的位置去复制函数签名,我觉得这非常不方便。如果每个接口需要实现10个函数,那我会直接崩溃。本来很好的思想,但实现起来很痛苦。我不得不做大量的复制和跳转操作。
所以
1.这可以仅仅作为语法糖,目的是通过 IDE 的快捷方式来减少不必要的工作。当使用IDE 编码的时候,IDE 也可以通过快捷键直接为我们生成代码。但是如果我在 txt 文件里面直接编码,或者IDE 直接支持实现接口快捷键。就无所谓了。
2.我觉得这可以让代码结构更加清晰

<哪位老哥英语好的再帮我解释下>

@bronze1man
Copy link
Contributor

bronze1man commented Nov 17, 2019

Looks like you need buy a good ide (like GoLand):
https://www.jetbrains.com/go/

it can generate all code in an interface for you.
建议你试试 GoLand ,似乎这个ide可以满足你的需求.

@fuyao-w
Copy link
Author

fuyao-w commented Nov 17, 2019

Looks like you need buy a good ide (like GoLand):
https://www.jetbrains.com/go/

it can generate all code in an interface for you.
建议你试试 GoLand ,似乎这个ide可以满足你的需求.

我一直在使用这个IDE,但是没找到快速生成实现接口代码的方法。能指导一下吗
I have been using this IDE all the time, but I haven't found a way to quickly generate implementation interface code.Can you guide me?

@bronze1man
Copy link
Contributor

请在 https://www.jetbrains.com/go/features/ 页面搜索 'interface' .
你会发现一个如果使用该功能的例子.

@fuyao-w
Copy link
Author

fuyao-w commented Nov 17, 2019

请在 https://www.jetbrains.com/go/features/ 页面搜索 'interface' .
你会发现一个如果使用该功能的例子.

谢谢指导,我指导该怎么做了

@fuyao-w fuyao-w closed this as completed Nov 17, 2019
@golang golang deleted a comment Nov 17, 2019
@golang golang locked and limited conversation to collaborators Nov 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

4 participants