最佳的方式来组织一个Go接口

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

Best way to organize a Go interface

问题

自从我上次用C++编程以来已经过了很长时间,但我知道在C++中,类被组织成.h文件和.cpp文件。此外,许多其他语言也通过将代码分割成逻辑分组来提高组织性,放在一个目录结构中。

现在我正在尝试学习Go语言,我正在阅读《Go for C++ Programmers》这篇文章,其中提到了接口。文章解释说,在Go语言中,接口实际上取代了类,并展示了如何很好地设置它们。

但我想弄清楚的是,我应该如何将接口组织到文件中?例如,接口应该放在一个文件中,而实现应该放在另一个文件中吗?

myInterface.go

type myInterface interface {
    get() int
    set(i int)
}

<br />myImplementation.go

type myType struct { i int }
func (p *myType) set(i int) { p.i = i }
func (p *myType) get() int { return p.i }

我的代码可能有错误,因为我还不完全了解我在做什么(如果我错了,请纠正我),但这是设置这个的最佳方式吗?我在如何组织Go代码方面遇到了很大的困难,所以任何帮助都将不胜感激!

Metropolis

英文:

Its been a long time since I have programmed in C++, but I know that in C++ the classes are organized into .h files and .cpp files. Also many other languages benefit from splitting up code into logical groupings within a directory structure to improve organization.

Well I am trying to learn Go now and I was reading over the <a href="http://golang.org/doc/go_for_cpp_programmers.html">Go for C++ Programmers</a> article when I came upon interfaces. The article explains that interfaces in Go essentially take the place of classes, and shows how to set them up pretty well.

What I am trying to figure out though is how should I organize an interface into files? For instance, should the interface be in one file while the implementation is in another?

myInterface.go

type myInterface interface {
    get() int
    set(i int)
}

<br />myImplementation.go

type myType struct { i int }
func (p *myType) set(i int) { p.i = i }
func (p *myType) get() int { return p.i }

My code here may be wrong since I do not completely know what I am doing yet (and if I am wrong please correct me), but would this be the best way to set this up? Im having a very hard time trying to wrap my head around how to organize code in Go so any help is appreciated!

Metropolis

答案1

得分: 16

没有必要将类型和接口放在单独的文件中。每个导出的内容才是重要的,你可以通过将名称的首字母大写来表示这些内容。在C和其他语言中,放入头文件的内容很重要,因为那是被“导入”(包含)的东西。在Go中,被导入的是包,而不管其内容如何组织到不同的源文件中(对导入者来说,这些内容都不可见)。

我个人的建议是避免创建不必要的文件。如果代码相对较短,可以将其放在一个文件中。如果代码很长,可以考虑将其中一部分拆分出来,例如接口+相关函数,如果你在Java或C++中进行开发,可能会形成一个单独的类。不要仅仅为了将定义与代码分离而拆分任何内容;尽管在C中有这样的需求,但在Go中没有意义。

英文:

There is no need to put types and interfaces in separate files. The things exported from each package are what matters and you denote those by beginning the name with a capital letter. In C & co. what goes into a header file matters because that's the thing “imported” (included). In Go it's the package that is imported, and it doesn't matter how its contents are organised into different source files (it won't be visible to the importer anyhow).

My personal recommendation is to avoid creating unnecessary files. If the code is relatively short, keep it in one file. If it's long, then consider splitting off parts for which it feels natural to do so (e.g. interface + related functions that would be probably form a separate class if you were doing it in Java or C++). Don't split off anything just for the sake of separating definitions from code; it doesn't make sense in Go even though it does in C.

答案2

得分: 7

Go编程语言既不是C++,也不是C。将其视为一种新语言来对待。Go没有等同于头文件的概念。它只与C++中的类的概念有一定的关联。由于您不是专业的C++程序员,可以忽略《面向C++程序员的Go语言》一文。

首先阅读《Go编程语言教程》,然后阅读《Effective Go》。浏览《Go编程语言规范》,这样您就会知道在哪里查找相关内容。

Go是开源的,所以可以查看真实的Go包文档源代码

首先,查看由Go的作者编写的time包的文档源代码。他们不使用单独的文件来声明和实现接口,那么为什么您要这样做呢?正如您自己所说,您还不知道自己在做什么,为什么不先按照专家的示例开始呢?

Go编程语言的开发动机之一是希望创建一种比C更好、比C++更简单的语言。考虑到Go的过程特性(通常类似于C),过程化设计概念如耦合和内聚是有用的。这些概念在将Go的time包分成多个源文件时是明显的。一些类设计概念也将是有用的,但请记住,Go不支持继承。

正如尼古拉斯·维尔特在他的经典论文《逐步细化的程序开发》中指出的,程序的早期草稿很少是理想的,有时甚至是粗糙的。即使最终版本也很少是完美的。例如,Go的作者们在仅仅几个月后就重写了Go的json包。

Go编程语言的设计和实现适合使用许多小函数。它倾向于简洁的解决方案。当然,许多函数在包外部是不可见的。在任何语言中,对函数大小或数量的任意限制很少有效。

> Go程序是通过链接包来构建的。一个包由一个或多个源文件构成,这些文件一起声明了属于该包的常量、类型、变量和函数,并且可以在同一包的所有文件中访问。这些元素可以被导出并在另一个包中使用。包,Go编程语言规范。

您希望您的第一个Go包要做什么?提出可以回答的具体问题,并提供详细信息。

英文:

The Go programming language is not C++, nor is it C. Approach it like a new language. Go has no equivalent to header files. It has only a loose association with the C++ concept of classes. Since you are not an expert C++ programmer, ignore the Go For C++ Programmers article.

Start by reading A Tutorial for the Go Programming Language. Then read Effective Go. Browse through The Go Programming Language Specification, so you'll know where to look things up.

Go is open source, so look at real Go package documentation and source code.

To start, look at the time package documentation and source code, which was written by the authors of Go. They don't use separate files for interface declarations and implementations, so why do you want to? As you say yourself, you don't know what you are doing yet, so why not start by following the example set by experts?

Some of the motivation for the development of the Go programming language came from a desire to create language which was better than C and much simpler than C++. Taking into account the procedural characteristics (often C like) of Go, procedural design concepts such as coupling and cohesion are useful. These concepts are evident in the partitioning of the Go time package into multiple source files. Some class design concepts will be useful too, but remember, Go does not support inheritance.

As Nicklaus Wirth pointed out in his classic paper, Program Development by Stepwise Refinement, early drafts of a program are rarely ideal, perhaps even sloppy sometimes. Even the final version is rarely perfect. For example, the Go authors, after only a few months, recently rewrote the Go json package.

The design and implementation of the Go programming language lends itself to the use of many small functions. It favors succinct solutions. Of course, many of the functions are not exposed outside the package. Arbitrary limits on function size or number rarely work in any language.

> Go programs are constructed by linking
> together packages. A package in turn
> is constructed from one or more source
> files that together declare constants,
> types, variables and functions
> belonging to the package and which are
> accessible in all files of the same
> package. Those elements may be
> exported and used in another package.
> Packages, The Go Programming Language
> Specification.

What do want your first Go package to do? Ask specific questions that can be answered, and provide details.

huangapple
  • 本文由 发表于 2010年5月22日 06:38:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/2886055.html
匿名

发表评论

匿名网友

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

确定