"Golang is more productive because any type can be given methods". How does that increase productivity?

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

"Golang is more productive because any type can be given methods". How does that increase productivity?

问题

我正在为您翻译以下内容:

我正在观看《Google IO 2010 - GO编程视频》,他们在视频中声称(参见32:38):

“GO可以使编程非常高效,因为

  • 任何类型都可以被赋予方法,这打开了有趣的设计可能性。”

有人可以解释一下这个特性如何提高生产力吗?其他编程语言中是否没有这个特性?

英文:

I was watching <a href="https://www.youtube.com/watch?v=jgVhBThJdXc">Google IO 2010 - GO Programming video</a> where they claim (see 32:38)

"GO can make programming very productive because

  • any type can be given methods, which opens up interesting design possibilities"

Can somebody please explain how can this feature increase productivity? Is this feature not present in other languages?

答案1

得分: 4

在Java或C#中,你需要声明一个类来继承/实现某个接口。但在Go语言中,不需要进行声明,只需为你想要实现的接口编写方法即可。

这种方式更加灵活,我想这在某种程度上提高了生产力。

英文:

in java or c#, you need declare a class to inherit/implement from some interface. but in golang, the declaration is not required, just write the methods for the interfaces you want to implement.

it is more flexible, so it increases productivity in some way, I suppose.

答案2

得分: 3

实际上,这意味着你可以为预定义类型添加方法(实际上不是直接为它们添加方法,而是为从它们派生的类型添加方法)。

例如,如果你有一个 Foo 的列表(其中 Foo 是你或其他包定义的类型),并且想要使该列表可排序,你只需要让该列表实现 sort.Interface 接口。为此,你只需定义一个新类型:

type fooSlice []Foo

并在其上定义方法来实现接口:

func (fs fooSlice) Len() int { return len(fs) }
func (fs fooSlice) Swap(a, b int) { fs[a], fs[b] = fs[b], fs[a] }
func (fs fooSlice) Less(a, b int) bool { return fs[a].Ord() < fs[b].Ord() }

然后,在任何你有 Foo 切片的地方,你可以将其转换为 fooSlice 并与 sort.Sort 一起使用:

func sortAndDoStuff(foos []Foo) {
    sort.Sort(fooSlice(foos))
    doStuff(foos)
}

如果用 Java 来做这个,需要更多的样板代码,因为你需要一个新的类,让它有一个字段([]Foo),声明它实现一个接口,实现相应的方法(就像 Go 语言中的做法),并编写在 []FoofooSlice 类之间进行转换的方法。

英文:

In practice, that means you can add methods to predefined types (well, not really them, but types derived from them).

For instance, if you have a list of Foo (where Foo is a type you or another package defined) and want to make that list sortable, you just have to make the list implement sort.Interface. To do so, you just define a new type

type fooSlice []Foo

and define methods on it to implement the interface :

func (fs fooSlice) Len() int { return len(fs) }
func (fs fooSlice) Swap(a, b int) { fs[a], fs[b] = fs[b], fs[a] }
func (fs fooSlice) Less(a, b int) bool { return fs[a].Ord() &lt; fs[b].Ord() }

Then, everywhere you have a slice of Foo, you can cast it as a fooSlice and use it with sort.Sort :

func sortAndDoStuff(foos []Foo) {
    sort.Sort(fooSlice(foos))
    doStuff(foos)
}

Doing that with, say, Java, would require a lot more plumbing, because you would need a new class, make it have one field (the []Foo), say it implements an interface, implement said methods (as with go) and make methods that goes back and forth between []Foo and fooSlice classes.

huangapple
  • 本文由 发表于 2015年12月2日 18:43:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/34040736.html
匿名

发表评论

匿名网友

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

确定