烂翻译系列之Golang新手入门——教程:入门——在另一个模块调用你的代码
In the previous section, you created a greetings module. In this section, you'll write code to make calls to the Hello function in the module you just wrote. You'll write code you can execute as an application, and which calls code in the greetings module.
在前一节,你创建了greetings模块。在本节中,你将编写代码调用该模块中的Hello函数。你将编写可执行应用,调用greetings模块中代码。
Note: This topic is part of a multi-part tutorial that begins with Create a Go module.
注意:本主题是多步教程的一部分,此教程从创建一个Go模块开始。
- Create a hellodirectory for your Go module source code. This is where you'll write your caller. 为你的Go模块源代码创建一个hello文件夹。你将在这里编写调用者代码。After you create this directory, you should have both a hello and a greetings directory at the same level in the hierarchy, like so: 在创建此文件夹后,你应该有两个同级文件夹hello和greetings,像下面这样: <home>/ |-- greetings/ |-- hello/ For example, if your command prompt is in the greetings directory, you could use the following commands: 举例,如果你的命令提示符当前路径是greetings文件夹,你可以使用以下命令: cd .. mkdir hello cd hello 
- Enable dependency tracking for the code you're about to write.  为将要编写的代码启用依赖跟踪。
To enable dependency tracking for your code, run the go mod initcommand, giving it the name of the module your code will be in. 要启用依赖跟踪,执行go mod init <模块名称>命令。For the purposes of this tutorial, use example.com/hellofor the module path. 本教程中,使用example.com/hello作为模块路径。$ go mod init example.com/hello go: creating new go.mod: module example.com/hello
- In your text editor, in the hello directory, create a file in which to write your code and call it hello.go. 在文本编辑器中,在hello文件夹中,创建一个名为hello.go的文件,你将在此文件中编码。
- Write code to call the Hellofunction, then print the function's return value. 编码调用Hello函数,然后打印函数的返回值。To do that, paste the following code into hello.go. 为此,粘贴以下代码到hello.go文件。 package main import ( "fmt" "example.com/greetings" ) func main() { // Get a greeting message and print it. message := greetings.Hello("Gladys") fmt.Println(message) } In this code, you: 在此代码中,你: - Declare a mainpackage. In Go, code executed as an application must be in amainpackage. 声明main包。在Go中,作为应用的可执行代码必须在main包中。
- Import two packages: example.com/greetingsand thefmtpackage. This gives your code access to functions in those packages. Importingexample.com/greetings(the package contained in the module you created earlier) gives you access to theHellofunction. You also importfmt, with functions for handling input and output text (such as printing text to the console). 导入两个包:example.com/greetings和fmt包。这赋予你的代码访问那些包中的函数的能力。导入example.com/greetings(此包包含在你先前创建的模块)赋予你访问Hello函数的能力。你也导入了包含处理输入文本和输出文本(比如打印文本到控制台)的函数的fmt包。
- Get a greeting by calling the greetingspackage’sHellofunction. 调用greetings包的Hello函数获取问候语。
 
- Declare a 
- Edit the example.com/hellomodule to use your localexample.com/greetingsmodule. 编辑example.com/hello模块来使用本地example.com/greetings模块。For production use, you’d publish the example.com/greetingsmodule from its repository (with a module path that reflected its published location), where Go tools could find it to download it. For now, because you haven't published the module yet, you need to adapt theexample.com/hellomodule so it can find theexample.com/greetingscode on your local file system. 对于生产环境,你可能从greetings的仓库发布example.com/greetings模块(模块路径反映它的发布位置),Go工具可以找到并下载它。目前,因为你还未发布模块,你需要改写example.com/hello模块,使其可以在本地文件系统中找到example.com/greetings代码。To do that, use the go mod editcommand to edit theexample.com/hellomodule to redirect Go tools from its module path (where the module isn't) to the local directory (where it is). 要这样做,使用go mod edit命令编辑example.com/hello模块去将Go工具从模块路径(非模块真正位置)重定向到本地目录(模块位置)。- From the command prompt in the hello directory, run the following command:  将命令提示符中定位到hello文件夹,执行以下命令:
$ go mod edit -replace example.com/greetings=../greetings The command specifies that example.com/greetingsshould be replaced with../greetingsfor the purpose of locating the dependency. After you run the command, the go.mod file in the hello directory should include areplacedirective: 此命令指定使用../greetings替换example.com/greetings来定位依赖关系。在执行此命令后,hello文件夹中的go.mod文件应该包括replace指令:module example.com/hello go 1.16 replace example.com/greetings => ../greetings 
- From the command prompt in the hello directory, run the go mod tidycommand to synchronize theexample.com/hellomodule's dependencies, adding those required by the code, but not yet tracked in the module. 将命令提示符中定位到hello文件夹,执行go mod tidy命令来同步example.com/hello模块的依赖,添加代码需要但仍未在模块中跟踪的依赖。$ go mod tidy go: found example.com/greetings in example.com/greetings v0.0.0-00010101000000-000000000000 After the command completes, the example.com/hellomodule's go.mod file should look like this: 在命令完成后,example.com/hello模块的go.mod文件应该看上去像这样:module example.com/hello go 1.16 replace example.com/greetings => ../greetings require example.com/greetings v0.0.0-00010101000000-000000000000 The command found the local code in the greetings directory, then added a requiredirective to specify thatexample.com/hellorequiresexample.com/greetings. You created this dependency when you imported thegreetingspackage in hello.go. 此命令在greetings文件夹找到本地代码,然后添加require指令来指定example.com/hello需要example.com/greetings。当你在hello.go导入greetings包时创建此依赖。The number following the module path is a pseudo-version number -- a generated number used in place of a semantic version number (which the module doesn't have yet). 模块路径后面的数字是伪版本号数字——用于代替语义版本号(模块还没有语义版本号)的一个生成码。 To reference a published module, a go.mod file would typically omit the replacedirective and use arequiredirective with a tagged version number at the end. 要引用一个发布的模块,go.mod文件通常删除replace指令并使用带有标签版本号的require指令。require example.com/greetings v1.1.0For more on version numbers, see Module version numbering. 关于版本号的更多信息,请看模块版本号。 
 
- From the command prompt in the hello directory, run the following command:  将命令提示符中定位到hello文件夹,执行以下命令:
- At the command prompt in the hellodirectory, run your code to confirm that it works. 将命令提示符中定位到hello文件夹,运行你的代码来确认它可以工作。$ go run . Hi, Gladys. Welcome!
Congrats! You've written two functioning modules.
恭喜!你已经编写了两个功能模块。
In the next topic, you'll add some error handling.
下个主题,你将增加一些错误处理。
 

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号