|
1 |
| -# Modules / Packages |
2 |
| -## Spec & Practice |
| 1 | +# 模块 / 包 |
| 2 | +## 规范与实践 |
3 | 3 | **JS**
|
4 | 4 |
|
5 |
| -As of es6, the Javascript spec includes a module system, however the external specs of AMD and CommonJS are also popular since the language began to address this issue rather late. |
| 5 | +从ES6开始,Javascript规定了一个模块引用模式,不过在这之前,AMD和CommonJS规范已经非常流行了。 |
6 | 6 |
|
7 |
| -Before es6 modules, the spec only supported the *script* mode, of which every file shares the same top-level global scope. This means that there was no official "file scope" for scripts. In practice, file-module scope was common since it was either introduced by code (`window.moduleA = …`), an external tool (requireJS), or by a runtime that baked-in a module system (NodeJS). |
| 7 | +在ES6模块化之前,规范只支持 *script* 模式,其中每个文件共享相同的顶级全局作用域。这意味着脚本没有正式的 "文件作用域"。实际上,文件模块作用域是很常见的,不管是通过代码(`window.moduleA = …`),或者外部工具(requireJS),还是在运行时中的模块系统(NodeJS) |
8 | 8 |
|
9 |
| -Therefore, it is safe to say that Javascript programs are commonly structured with a 1-to-1 relationship between files and modules with local scope. |
| 9 | +因此,可以肯定地说,Javascript程序通常使用具有本地作用域的文件与模块一对一对应的形式构建 |
10 | 10 |
|
11 | 11 | **Go**
|
| 12 | +Go 从一开始就支持了import声明和包的支持。在Go中没有文件作用域,只有包作用域。从Go 1.6 (or 1.5 + flag)开始,对把依赖包放到项目目录中的 [vendor folder](https://blog.gopheracademy.com/advent-2015/vendor-folder/) 提供了更好的支持。然而它没有试图解决任何问题: |
| 13 | + |
12 | 14 |
|
13 |
| -Go's import statement and package support were part of the spec from the beginning. In Go there is no file scope, only package scope. As of Go 1.6 (or 1.5 + flag), there's better support for encapsulating dependent packages inside a project with the [vendor folder](https://blog.gopheracademy.com/advent-2015/vendor-folder/). However, it doesn't attempt to solve everything: |
14 | 15 | > … this does not attempt to solve the problem of vendoring resulting in multiple copies of a package being linked into a single binary. Sometimes having multiple copies of a library is not a problem; sometimes it is. At least for now, it doesn’t seem that the go command should be in charge of policing or solving that problem.
|
| 16 | +> |
| 17 | +> 它没有试图解决同一个第三方包被多次拷贝的问题。有些时候存在多拷贝不是一个问题,但是有些时候它是个问题。至少现在看来,Go并没有想负责解决这个问题。 |
| 18 | +
|
| 19 | +如果选择使用Go模块(如go 1.11中介绍的),vendor文件夹默认是不必要的。(下载的模块源代码被存在目录`$GOPATH/pkg/mod`)。`go.mod`和`go.sum`替代了vendor文件夹,这两个文件提供 100% 可复制和构建安全(提供获取扩展模块的方式)。不过你也可以[保留](https://github.com/golang/go/wiki/Modules#how-do-i-use-vendoring-with-modules-is-vendoring-going-away) vendor文件夹 。 |
| 20 | + |
| 21 | + |
| 22 | +**不同点** |
15 | 23 |
|
16 |
| -If choosing to use go modules, as introduced in Go 1.11, then it will make the vendor folder redundant by default (downloaded modules source code is stored at `$GOPATH/pkg/mod`). In exchange for the vendor folder, are `go.mod` and `go.sum` which together provide a 100% reproducible and secure builds (provided the means to fetch external modules). There's an [option](https://github.com/golang/go/wiki/Modules#how-do-i-use-vendoring-with-modules-is-vendoring-going-away) to keep using the vendor folder with modules. |
| 24 | +一个 JS 模块可以使任意有效的 JS类型。通过导出一个object,他可以将将多个函数、变量等放到*package*里。如果导出一个函数,它可以只具备这个函数的功能。另一方面,一个Go package,就像他的名字一样,只是一个包(译者注:老外真能瞎扯)。所以如果一个Javascript模块是一个函数类型,那么它可以直接被调用,这在Go里面是不可以的。 |
17 | 25 |
|
18 |
| -**The differences** |
| 26 | +另外一个不同点是同一个项目中其他内部组件的调用成本。在Javascript中,因为每个文件都是一个模块(通常是这样),所以文件都需要被导入后才能使用。另一方面,在Go中,因为没有文件作用域,所有在同一包中的文件相互之间可以任意访问。 |
19 | 27 |
|
20 |
| -A Javascript module can be any valid Javascript type. By exporting an object, it can *package* multiple functionalities. By exporting a function it can surface a single functionality. On the other hand, a Go package, is as its a name- just a package. So while a Javascript module can be directly invoked if it is a function type, this is not a possibility with a Go package. |
| 28 | +## 包管理 |
21 | 29 |
|
22 |
| -Another difference is the consumption of other internal components within your project. In Javascript, since each file is (usually) a module, then each of the files that were decoupled from the current file must be imported. On the other hand, in Go, all files within the same package can have access to each other since there is no file scope. |
| 30 | +在Javascript中,**NPM** 是NodeJS的包管理工具,在客户端(译者注:浏览器)也几乎是如此。**Bower**在客户端也是一个流行的包管理软件 |
23 | 31 |
|
24 |
| -## Management |
| 32 | +Go 1.11 引入了 [go mod](https://github.com/golang/go/wiki/Modules), 算是一个正式的依赖管理工具。 |
25 | 33 |
|
26 |
| -For Javascript development, NPM is the defacto dependency manager for NodeJS, and may also be used for client-side projects. Bower is also a popular for client-side projects. |
| 34 | +由于这是一个后期补充,所以其他社区的解决方案也在一些项目中使用。如下列表是部分的这些社区的解决方案: |
27 | 35 |
|
28 |
| -Go 1.11 introduced [go mod](https://github.com/golang/go/wiki/Modules), the official dependency manager. |
29 |
| -As this was a late addition to the ecosystem, other community driven solutions are still in-use in some projects. Here is a partial list of these community solutions: |
30 | 36 | - https://github.com/Masterminds/glide
|
31 | 37 | - https://github.com/tools/godep
|
32 | 38 | - https://github.com/kardianos/govendor
|
|
0 commit comments