烂翻译系列之Golang新手入门——教程:创建模块

Table of Contents  目录

Prerequisites  前提条件
Start a module that others can use  开始一个其他人可使用的模块

This is the first part of a tutorial that introduces a few fundamental features of the Go language. If you're just getting started with Go, be sure to take a look at Tutorial: Get started with Go, which introduces the go command, Go modules, and very simple Go code.

这是介绍Go语言基本特性的教程的第一部分。如果你刚开始使用Go,务必看一下教程:开始使用Go,它介绍了go命令,Go模块,和特别简单的Go代码。

In this tutorial you'll create two modules. The first is a library which is intended to be imported by other libraries or applications. The second is a caller application which will use the first.

在本教程中你将创建两个模块。第一个模块是个类库,供其它类库或应用导入。第二个模块是个调用者应用,它将使用第一个模块。

This tutorial's sequence includes seven brief topics that each illustrate a different part of the language.  本教程包括七个简短主题,每个主题阐述语言的不同部分。

  1. Create a module -- Write a small module with functions you can call from another module.  创建一个模块——编写一个小模块,里面含有你可以在其它模块中调用的函数。
  2. Call your code from another module -- Import and use your new module.  在其它模块中调用你的代码——导入并使用你的新模块。
  3. Return and handle an error -- Add simple error handling.  返回并处理错误——添加简单错误处理。
  4. Return a random greeting -- Handle data in slices (Go's dynamically-sized arrays).  返回一个随机问候语——处理切片中的数据(Go的动态数组)。
  5. Return greetings for multiple people -- Store key/value pairs in a map.  为多个人返回问候语——在映射中存储键/值对。
  6. Add a test -- Use Go's built-in unit testing features to test your code.  增加测试——使用Go的内置单元测试特性来测试你的代码。
  7. Compile and install the application -- Compile and install your code locally.  编译并安装应用——在本地编译并安装你的代码
Note: For other tutorials, see Tutorials.
注意:其它教程,请看教程。

Prerequisites  前提条件

  • Some programming experience. The code here is pretty simple, but it helps to know something about functions, loops, and arrays.  一些编程经验。这里的代码是极其简单的,但了解一些函数,循环,和数组相关知识,是有帮助的。
  • A tool to edit your code. Any text editor you have will work fine. Most text editors have good support for Go. The most popular are VSCode (free), GoLand (paid), and Vim (free).  一款编辑代码的工具。任何你可接受的文本编辑器。许多文本编辑器为Go提供了良好的支持。非常受欢迎的有VSCode(免费),GoLand(付费),和Vim(免费)。
  • A command terminal. Go works well using any terminal on Linux and Mac, and on PowerShell or cmd in Windows.  一个命令终端。使用Linux和Max上的任何终端和Windows上的PowerShell或cmd,Go都工作的很好。

Start a module that others can use   开始一个其他人可使用的模块

Start by creating a Go module. In a module, you collect one or more related packages for a discrete and useful set of functions. For example, you might create a module with packages that have functions for doing financial analysis so that others writing financial applications can use your work. For more about developing modules, see Developing and publishing modules.

创建一个Go模块作为开始,你将分散且有用的一系列函数收集到一个或多个包里。例如,你可能会创建一个具有财务分析功能的模块,其他编写财务分析应用的人可以使用你的成果。更多开发模块相关信息,请看开发并发布模块。

Go code is grouped into packages, and packages are grouped into modules. Your module specifies dependencies needed to run your code, including the Go version and the set of other modules it requires.

Go代码被分组到包中,且包被分组模块中。模块指定代码需要的依赖,包括Go版本和该模块需要的其它模块。

As you add or improve functionality in your module, you publish new versions of the module. Developers writing code that calls functions in your module can import the module's updated packages and test with the new version before putting it into production use.

在你添加或改进模块中的功能,你发布模块的新版本。开发者编写调用你模块中函数的代码,可以导入模块的更新包并在投入生产前使用新版本测试。

  1. Open a command prompt and cd to your home directory.  打开命令行提示符并进入你的主目录。

    On Linux or Mac:  在Linux或Mac上:

    cd

    On Windows:  在Windows上:

    cd %HOMEPATH%
  2. Create a greetings directory for your Go module source code.  为你的Go模块源代码创建greetings文件夹

    For example, from your home directory use the following commands:  例如,在你的主目录下使用如下命令:

    mkdir greetings
    cd greetings
  3. Start your module using the go mod init command.  使用go mod init命令开始你的模块。

    Run the go mod init command, giving it your module path -- here, use example.com/greetings. If you publish a module, this must be a path from which your module can be downloaded by Go tools. That would be your code's repository.  执行go mod init命令,参数为模块路径——这里,使用example.com/greetings。如果你发布一个模块,这里必须是通过Go工具可以下载模块的路径。那将是你代码的仓库。

    For more on naming your module with a module path, see Managing dependencies.  关于使用模块路径命名模块的更多信息,请看管理依赖。

    $ go mod init example.com/greetings
    go: creating new go.mod: module example.com/greetings

    The go mod init command creates a go.mod file to track your code's dependencies. So far, the file includes only the name of your module and the Go version your code supports. But as you add dependencies, the go.mod file will list the versions your code depends on. This keeps builds reproducible and gives you direct control over which module versions to use.  go mod init命令创建一个名为go.mod的文件来跟踪代码的依赖。目前,该文件仅包括模块名称和代码支持的Go版本。但随着增加依赖,go.mod文件将列出代码依赖的模块版本。这保持构建的可复制性和赋予你直接控制使用哪个模块的能力。

  4. In your text editor, create a file in which to write your code and call it greetings.go.  在文本编辑器中,创建greetings.go文件,你将在此文件中编码。
  5. Paste the following code into your greetings.go file and save the file.  粘贴以下代码到greetings.go文件并且保存文件。
    package greetings
    
    import "fmt"
    
    // Hello returns a greeting for the named person.
    func Hello(name string) string {
        // Return a greeting that embeds the name in a message.
        message := fmt.Sprintf("Hi, %v. Welcome!", name)
        return message
    }

    This is the first code for your module. It returns a greeting to any caller that asks for one. You'll write code that calls this function in the next step.  这是你模块的首次代码。它为任意调用者返回问候语。你将在下一步编写调用此函数的代码。

    In this code, you:  在本代码中,你:

    • Declare a greetings package to collect related functions.  声明了一个greetings包来汇聚关联功能。
    • Implement a Hello function to return the greeting.  实现Hello函数来返回问候语。

      This function takes a name parameter whose type is string. The function also returns a string. In Go, a function whose name starts with a capital letter can be called by a function not in the same package. This is known in Go as an exported name. For more about exported names, see Exported names in the Go tour.  此函数有一个string类型的参数name。此函数也返回一个字符串。在Go中,一个以大写字母开头的函数能在其它包里调用。这在Go中作为导出名称是众所周知的。请看Go指南中的导出名称。

    • Declare a message variable to hold your greeting.  声明一个message变量来保存问候语:

      In Go, the := operator is a shortcut for declaring and initializing a variable in one line (Go uses the value on the right to determine the variable's type). Taking the long way, you might have written this as:  在Go中,:=操作符是在一行中声明且初始化变量的捷径(Go使用右边的值来推测变量的类型)。复杂一点,你可能会如此编码:

      var message string
      message = fmt.Sprintf("Hi, %v. Welcome!", name)
    • Use the fmt package's Sprintf function to create a greeting message. The first argument is a format string, and Sprintf substitutes the name parameter's value for the %v format verb. Inserting the value of the name parameter completes the greeting text.  使用fmt包的Sprintf函数来创建一条问候语消息。第一个参数是格式化字符串,Sprintf以name参数的值取代%v格式动词。插入参数name的值来完成问候语文本。
    • Return the formatted greeting text to the caller.  给调用者返回格式化的问候语文本。

In the next step, you'll call this function from another module.

下一步,你将从另一个模块调用此函数。

posted @ 2022-01-08 16:46  菜鸟吊思  阅读(323)  评论(0)    收藏  举报