GO-0012
原文
Tutorial: Get started with Go
Table of Contents
Prerequisites
Call code in an external package
In this tutorial, you'll get a brief introduction to Go programming. Along the way, you will:
- Install Go (if you haven't already).
- Write some simple "Hello, world" code.
- Use the
gocommand to run your code. - Use the Go package discovery tool to find packages you can use in your own code.
- Call functions of an external module.
Note: For other tutorials, see Tutorials.
Prerequisites¶
- Some programming experience. The code here is pretty simple, but it helps to know something about functions.
- 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.
Install Go¶
Just use the Download and install steps.
Write some code¶
Get started with Hello, World.
-
Open a command prompt and cd to your home directory.
On Linux or Mac:
cdOn Windows:
cd %HOMEPATH% -
Create a hello directory for your first Go source code.
For example, use the following commands:
mkdir hello cd hello -
Enable dependency tracking for your code.
When your code imports packages contained in other modules, you manage those dependencies through your code's own module. That module is defined by a go.mod file that tracks the modules that provide those packages. That go.mod file stays with your code, including in your source code repository.
To enable dependency tracking for your code by creating a go.mod file, run the
go mod initcommand, giving it the name of the module your code will be in. The name is the module's module path.In actual development, the module path will typically be the repository location where your source code will be kept. For example, the module path might be
github.com/mymodule. If you plan to publish your module for others to use, the module path must be a location from which Go tools can download your module. For more about naming a module with a module path, see Managing dependencies.For the purposes of this tutorial, just use
example/hello.$ go mod init example/hello go: creating new go.mod: module example/hello -
In your text editor, create a file hello.go in which to write your code.
-
Paste the following code into your hello.go file and save the file.
package main import "fmt" func main() { fmt.Println("Hello, World!") }This is your Go code. In this code, you:
- Declare a
mainpackage (a package is a way to group functions, and it's made up of all the files in the same directory). - Import the popular
fmtpackage, which contains functions for formatting text, including printing to the console. This package is one of the standard library packages you got when you installed Go. - Implement a
mainfunction to print a message to the console. Amainfunction executes by default when you run themainpackage.
- Declare a
-
Run your code to see the greeting.
$ go run . Hello, World!The
go runcommand is one of manygocommands you'll use to get things done with Go. Use the following command to get a list of the others:$ go help
Call code in an external package¶
When you need your code to do something that might have been implemented by someone else, you can look for a package that has functions you can use in your code.
-
Make your printed message a little more interesting with a function from an external module.
- Visit pkg.go.dev and search for a "quote" package.
- In the search results, locate and click on the v1 of the
rsc.io/quotepackage (it should be listed with the "Other major versions" ofrsc.io/quote/v4). - In the Documentation section, under Index, note the list of functions you can call from your code. You'll use the
Gofunction. - At the top of this page, note that package
quoteis included in thersc.io/quotemodule.
You can use the pkg.go.dev site to find published modules whose packages have functions you can use in your own code. Packages are published in modules -- like
rsc.io/quote-- where others can use them. Modules are improved with new versions over time, and you can upgrade your code to use the improved versions. -
In your Go code, import the
rsc.io/quotepackage and add a call to itsGofunction.After adding the highlighted lines, your code should include the following:
package main import "fmt" import "rsc.io/quote" func main() { fmt.Println(quote.Go()) } -
Add new module requirements and sums.
Go will add the
quotemodule as a requirement, as well as a go.sum file for use in authenticating the module. For more, see Authenticating modules in the Go Modules Reference.$ go mod tidy go: finding module for package rsc.io/quote go: found rsc.io/quote in rsc.io/quote v1.5.2 -
Run your code to see the message generated by the function you're calling.
$ go run . Don't communicate by sharing memory, share memory by communicating.Notice that your code calls the
Gofunction, printing a clever message about communication.When you ran
go mod tidy, it located and downloaded thersc.io/quotemodule that contains the package you imported. By default, it downloaded the latest version -- v1.5.2.
Write more code¶
With this quick introduction, you got Go installed and learned some of the basics. To write some more code with another tutorial, take a look at Create a Go module.
1. 全文专业翻译(英译汉)
教程:Go 语言入门
目录
- 前置要求
- 安装 Go
- 编写一些代码
- 调用外部包中的代码
- 编写更多代码
在本教程中,您将获得 Go 编程的简要介绍。在此过程中,您将完成以下任务:
- 安装 Go(如果尚未安装);
- 编写简单的 “Hello, World” 代码;
- 使用
go命令运行您的代码; - 使用 Go 包发现工具查找可在您自己代码中使用的包;
- 调用外部模块中的函数。
注意:其他教程请参见 Tutorials。
前置要求
具备一定的编程经验。此处的代码非常简单,但了解函数的基本概念会有所帮助。
一个用于编辑代码的工具。任何文本编辑器均可胜任。大多数编辑器对 Go 有良好支持,最流行的包括 VSCode(免费)、GoLand(付费)和 Vim(免费)。
一个命令行终端。Go 在 Linux 和 macOS 的任意终端下运行良好,在 Windows 上则可使用 PowerShell 或 cmd。
安装 Go
直接按照“下载与安装”步骤操作即可。
编写一些代码
从 “Hello, World” 开始。
-
打开命令提示符,并切换到您的主目录。
-
在 Linux 或 Mac 上:
cd -
在 Windows 上:
cd %HOMEPATH%
-
-
为您的首个 Go 源代码创建一个
hello目录。例如,使用以下命令:mkdir hello cd hello -
为您的代码启用依赖项追踪。
当您的代码导入其他模块中的包时,您需通过自身代码所属的模块来管理这些依赖。该模块由一个go.mod文件定义,用于记录提供这些包的模块信息。此go.mod文件将随您的代码一同保存(包括纳入源代码仓库)。要通过创建
go.mod文件启用依赖追踪,请运行go mod init命令,并传入您的代码所属模块的名称(即模块路径)。在实际开发中,模块路径通常对应您源代码的托管位置(如
github.com/mymodule)。若您计划公开发布模块供他人使用,则模块路径必须是 Go 工具可从中下载该模块的位置。有关模块路径命名的更多说明,请参阅《依赖管理》文档。本教程中,仅需使用
example/hello即可:$ go mod init example/hello go: creating new go.mod: module example/hello -
在文本编辑器中创建文件
hello.go,用于编写代码。 -
将以下代码粘贴到
hello.go并保存:package main import "fmt" func main() { fmt.Println("Hello, World!") }这段 Go 代码包含以下内容:
- 声明一个
main包(包用于组织函数,由同一目录下的所有文件构成); - 导入广为人知的
fmt包(该包提供文本格式化功能,包括向控制台输出,属于安装 Go 时自带的标准库); - 实现
main函数,用于向控制台打印消息(运行main包时,main函数会默认执行)。
- 声明一个
-
运行代码以查看问候语:
$ go run . Hello, World!go run是众多go命令之一,用于完成各类 Go 开发任务。运行以下命令可查看其他可用命令:$ go help
调用外部包中的代码
当您的代码需要实现某项功能,而该功能可能已被他人实现时,您可以查找一个包含可用函数的包。
-
使用外部模块中的函数,让输出消息更有趣。
- 访问 pkg.go.dev,搜索 “quote” 包;
- 在搜索结果中,找到并点击
rsc.io/quote包的 v1 版本(应列在 “Other major versions” 下,与rsc.io/quote/v4并列); - 在页面“Documentation”部分的“Index”下,查看可调用的函数列表,您将使用
Go函数; - 注意页面顶部显示:
quote包属于rsc.io/quote模块。
您可通过 pkg.go.dev 查找已发布的模块,其包中的函数可被您的代码复用。包以模块形式发布(如
rsc.io/quote),供他人使用。模块会随时间推出新版本,您也可升级代码以使用改进后的版本。 -
在 Go 代码中导入
rsc.io/quote包,并添加对其Go函数的调用。
添加高亮行后,您的代码应如下所示:package main import "fmt" import "rsc.io/quote" func main() { fmt.Println(quote.Go()) } -
添加新的模块依赖和校验和。
Go 会自动将quote模块添加为依赖项,并生成go.sum文件用于模块认证。详情请参阅《Go 模块参考》中的“模块认证”章节。$ go mod tidy go: finding module for package rsc.io/quote go: found rsc.io/quote in rsc.io/quote v1.5.2 -
运行代码,查看所调用函数生成的消息:
$ go run . Don't communicate by sharing memory, share memory by communicating.注意:您的代码调用了
Go函数,输出了一句关于通信的巧妙格言。
执行go mod tidy时,Go 自动定位并下载了包含所导入包的rsc.io/quote模块(默认下载最新版本 v1.5.2)。
编写更多代码
通过这一快速入门,您已成功安装 Go 并掌握了基础知识。若想继续编写更多代码,请参阅另一篇教程:《创建 Go 模块》。
2. 词汇表梳理
本次共收获单词:18 个
| 序号 | 单词 | 词性 | 音标 | 拼读 | 释义 |
|---|---|---|---|---|---|
| 1 | Prerequisites | 名词(复数) | /ˌpriːˈrekwɪzɪts/ | Pre·req·ui·sites 普-瑞-瑞-克-维-兹-茨 | 前置要求。指开始某项任务前必须满足的条件或具备的知识/工具。 |
| 2 | Pretty | 副词 | /ˈprɪti/ | Pret·ty 普-瑞-提 | 相当,颇为。用于口语化强调程度(此处修饰 simple,意为“相当简单”)。 |
| 3 | Locate | 动词 | /loʊˈkeɪt/ | Lo·cate 楼-凯-特 | 定位,找到。指在大量信息或文件系统中确定目标项的具体位置。 |
| 4 | Clever | 形容词 | /ˈklevər/ | Clev·er 克-雷-沃 | 巧妙的。指设计精巧、富有智慧或令人会心一笑的表达(如 quote 中的格言)。 |
| 5 | Experience | 名词 | /ɪkˈspɪriəns/ | Ex·pe·ri·ence 伊-克-斯-皮-瑞-恩-斯 | 经验。指通过实践、观察或参与而获得的知识、技能或理解,尤指在编程或软件开发领域的实际操作积累。 |
| 6 | Directory | 名词 | /dəˈrektəri/ | Di·rec·to·ry 德-瑞-克-特-瑞 | 目录。指文件系统中用于组织和存储文件及其他子目录的容器结构,在命令行操作和项目布局中具有核心作用。 |
| 7 | Track | 动词 | /træk/ | Track 特-拉-克 | 追踪,记录。在软件开发中特指对依赖项、版本、变更或状态进行持续监控和管理,确保系统可复现且一致。 |
| 8 | Actual | 形容词 | /ˈæktʃuəl/ | Ac·tu·al 艾-克-丘-欧 | 实际的。指在真实开发场景中发生或采用的做法,而非示例、假设或教学简化情形。 |
| 9 | Purpose | 名词 | /ˈpɜːrpəs/ | Pur·pose 珀-珀-斯 | 目的,用途。指某项操作、设计或约定所要达成的具体目标或意图,常用于限定上下文范围(如教程中的简化假设)。 |
| 10 | Section | 名词 | /ˈsekʃn/ | Sec·tion 赛-克-申 | 章节,部分。指文档、网页或技术资料中具有明确主题和标题的结构化内容单元。 |
| 11 | Improve | 动词 | /ɪmˈpruːv/ | Im·prove 伊-姆-普-鲁-夫 | 改进,优化。指通过修正缺陷、增强功能或提升性能,使软件模块、代码或系统变得更好。 |
| 12 | Quote | 名词 | /kwoʊt/ | Quote 克-沃-特 | 引语,格言。在 Go 教程上下文中,特指由 rsc.io/quote 包提供的、用于演示外部模块调用的预定义字符串常量。 |
| 13 | Highlighted | 形容词(过去分词作形容词) | /ˈhaɪˌlaɪtɪd/ | High·light·ed 海-莱-特-伊-德 | 高亮显示的。指在代码示例或文档中通过颜色、背景或字体加粗等方式特别标出的内容,用于引导读者关注新增、修改或关键部分。 |
| 14 | Requirement | 名词 | /rɪˈkwaɪərmənt/ | Re·quire·ment 瑞-克-怀-尔-门-特 | 依赖项,需求。在 Go 模块系统中,特指项目所依赖的外部模块及其版本信息,由 go.mod 文件声明并由 go mod tidy 自动管理。 |
| 15 | Sum | 名词 | /sʌm/ | Sum 萨-姆 | 校验和。在 Go 模块系统中,特指 go.sum 文件中记录的加密哈希值,用于验证所下载模块内容的完整性和真实性,防止篡改或意外损坏。 |
| 16 | Authenticating | 动词(现在分词/动名词) | /ɔːˈθentɪkeɪtɪŋ/ | Au·then·ti·ca·ting 奥-森-提-凯-廷 | 认证。在 Go 模块系统中,指通过比对 go.sum 文件中存储的加密校验和,验证所下载模块内容的真实性与完整性,确保其未被篡改或损坏。 |
| 17 | Typically | 副词 | /ˈtɪpɪkli/ | Typ·i·cal·ly 提-皮-克-利 | 通常,一般而言。用于描述在常规开发实践或标准场景下普遍采用的做法或默认行为,强调惯例而非绝对规则。 |
| 18 | Support | 名词 | /səˈpɔːrt/ | Sup·port 瑟-珀-特 | 支持。在开发工具语境中,指编辑器、IDE 或系统对特定语言(如 Go)提供的功能适配,包括语法高亮、自动补全、调试、格式化和错误检查等。 |

浙公网安备 33010602011771号