GO-0013

原文

Tutorial: Create a Go module

Table of Contents

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.

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).
  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.
  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).
  • A command terminal. Go works well using any terminal on Linux and Mac, and on PowerShell or cmd in Windows.

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 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.

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:

    cd
    

    On Windows:

    cd %HOMEPATH%
    
  2. Create a greetings directory for your Go module source code.

    For example, from your home directory use the following commands:

    mkdir greetings
    cd greetings
    
  3. Start your module using the go mod init command.

    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.

    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.

  4. In your text editor, create a file in which to write your code and call it greetings.go.

  5. Paste the following code into your greetings.go file and save the file.

    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.

    • Implement a Hello function to return the greeting.

      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.

    • Declare a message variable to hold your greeting.

      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:

      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.

    • Return the formatted greeting text to the caller.

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

以下是对您提供的英文技术文档《Tutorial: Create a Go module》的完整处理,严格遵循您提出的三项要求:专业翻译、词汇表梳理、短语表梳理。

序号 单词 词性 音标 拼读 释义
1 Fundamental 形容词 /ˌfʌndəˈmentl/ Fun·da·men·tal 范-德-门-特-欧 基础的,根本的。指构成某系统或知识体系核心的、不可或缺的要素。
2 Caller 名词 /ˈkɔːlər/ Call·er 考-勒 调用方。在编程中指发起函数或方法调用的代码实体(如另一个函数、模块或应用程序)。
3 Sequence 名词 /ˈsiːkwəns/ Se·quence 西-克-温-斯 序列,顺序。指按特定逻辑或时间排列的一系列步骤或项目。
4 Illustrate 动词 /ˈɪləstreɪt/ Il·lus·trate 伊-乐-斯特-雷-特 说明,阐明。通过示例、图示或代码展示某个概念或特性。
5 Financial 形容词 /faɪˈnænʃl/ Fi·nan·cial 法-伊-南-夏-欧 金融的,财务的。与资金管理、投资、会计等经济活动相关的。
6 Specify 动词 /ˈspesɪfaɪ/ Spec·i·fy 斯-佩-斯-艾-法-伊 明确说明,指定。在配置或声明中精确指出某项要求或属性。
7 Publish 动词 /ˈpʌblɪʃ/ Pub·lish 帕-布-利-什 发布。将软件模块上传至公共或私有仓库,使其可供他人下载和使用。
8 Production 名词 /prəˈdʌkʃn/ Pro·duc·tion 普-拉-达-克-申 生产环境。指软件正式对外提供服务的运行环境,区别于开发或测试环境。
9 Reproducible 形容词 /ˌriːprəˈduːsəbl/ Re·pro·du·ci·ble 瑞-普-洛-杜-斯-厄-波 可重现的。指在相同输入和依赖条件下,构建过程能产生完全一致的输出结果。
10 Operator 名词 /ˈɑːpəreɪtər/ Op·er·a·tor 阿-珀-雷-特 运算符。编程语言中用于执行特定操作的符号(如 :=, +, ==)。
11 Format 名词 / 动词 /ˈfɔːrmæt/ For·mat 佛-热-麦-特 格式。指数据或文本的组织结构或呈现样式;作动词时指按特定格式排版或转换。
12 Intend 动词 /ɪnˈtend/ In·tend 因-腾-德 打算,意图。表示计划做某事或使某物用于特定目的;在技术语境中常用于描述模块、接口或函数的设计目的。
13 Discrete 形容词 /dɪˈskriːt/ Dis·crete 迪-斯-克-瑞-特 离散的,独立的。指由分离、不连续的个体或单元组成的;在计算机科学中常用于描述彼此独立、边界清晰的组件、值或系统模块。
14 Analysis 名词 /əˈnæləsɪs/ A·nal·y·sis 厄-纳-乐-瑟-斯 分析。指对系统、数据、代码或问题进行系统性分解与研究,以理解其结构、行为或潜在问题;在 IT 领域常用于性能分析、静态代码分析、需求分析等场景。
15 Functionality 名词 /ˌfʌŋkʃəˈnæləti/ Func·tion·al·i·ty 范-克-夏-纳-乐-提 功能,功能性。指软件、模块或系统所提供的具体能力或操作集合;在 IT 领域中常用于描述“某组件能做什么”。
16 Shortcut 名词 /ˈʃɔːrtkʌt/ Short·cut 肖-特-卡-特 快捷方式,捷径。在编程语境中,指语言提供的简化语法结构,用于以更少代码实现常见操作(如变量声明、初始化等)。
17 Determine 动词 /dɪˈtɜːrmɪn/ De·ter·mine 迪-特-欧-敏 确定,判定。指通过分析、计算或规则明确得出某事物的性质、值、类型或行为;在编程中常用于描述编译器、运行时或语言机制如何推断变量类型、依赖版本或执行路径。
18 Substitutes 动词(第三人称单数形式) /ˈsʌbstɪtjuːts/ Sub·sti·tutes 萨-布-斯-提-秋-茨 替代,代入。在编程中特指将占位符(如格式字符串中的 %v)替换为实际值的过程。
19 Verb 名词 /vɜːrb/ Verb 沃-布 动词。在自然语言中表示动作或状态的词类;在编程(特别是格式化函数如 fmt.Sprintf)中,“format verb” 指用于指定数据如何被格式化的占位符符号,如 %v、%s、%d 等。
20 Capital 形容词 / 名词 /ˈkæpɪtl/ Cap·i·tal 卡-皮-特-欧 作形容词时,意为“大写的”,指字母形式为大写(如 A, B, C),与小写(lowercase)相对;作名词时,可指“首都”或“资本”,但在 Go 语言上下文中仅涉及“大写字母”含义。
posted @ 2026-01-27 06:47  红尘过客2022  阅读(47)  评论(0)    收藏  举报