烂翻译系列之Golang新手入门——教程:入门
Table of Contents 目录
Prerequisites 前提条件
Install Go 安装Go
Write some code 编写一些代码
Call code in an external package 调用外部包的代码
Write more code 编写更多代码
In this tutorial, you'll get a brief introduction to Go programming. Along the way, you will:
本教程中,你将对Go编程有简单的了解。沿着教程,你将:
- Install Go (if you haven't already). 安装Go(如果你未安装)
- Write some simple "Hello, world" code. 编写一些简单的“Hello, world”代码
- Use the gocommand to run your code. 使用go命令来运行你的代码
- Use the Go package discovery tool to find packages you can use in your own code. 使用Go包发现工具来查找可以在你的代码中使用的包
- 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). 一款编辑代码的工具。任何你可接受的文本编辑器。许多文本编辑器为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都工作的很好。
Install Go 安装Go
Just use the Download and install steps.
只需参考下载和安装步骤。
Write some code 编写一些代码
Get started with Hello, World.
从Hello, World开始。
- Open a command prompt and cd to your home directory.  打开命令行提示符并进入你的主目录。
On Linux or Mac: 在Linux或Mac上: cd On Windows: 在Windows上: cd %HOMEPATH% 
- Create a hello directory for your first Go source code.  为你的首个Go源代码创建一个hello文件夹。
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. 当你的代码导入其它模块中的包,通过你的代码的模块管理那些依赖。这个模块由一个名为go.mod的文件定义,它跟踪提供那些包的模块。 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. 要为你的代码启用依赖跟踪,通过执行go mod init <模块名称>命令,创建一个名为go.mod的文件。模块名称是模块的模块路径。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. 实际开发中,模块路径通常是你存放源代码的仓库位置。例如,模块路径可能是github.com/mymodule。如果你计划发布你的模块供其他人使用,模块路径必须是通过Go工具可以下载你的模块的位置。For the purposes of this tutorial, just use example/hello. 对于本教程,只需使用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. 在你的文本编辑器中,创建一个名为hello.go的文件,你将在此文件中编码。 
- 
Paste the following code into your hello.go file and save the file. 粘贴以下代码到hello.go文件并保存文件。 package main import "fmt" func main() { fmt.Println("Hello, World!") } This is your Go code. In this code, you: 这是你的Go代码。在这些代码中,你做了以下工作: - Declare a mainpackage (a package is a way to group functions, and it's made up of all the files in the same directory). 声明一个main包(包是分组功能的一种方式,它由与包对应的文件夹下的所有文件组成)
- 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. 导入常用的fmt包,它包含格式化文本的函数,包括向控制台打印。该包是标准库包之一,在安装Go后你就可以使用它。
- Implement a mainfunction to print a message to the console. Amainfunction executes by default when you run themainpackage. 实现main函数以打印消息到控制台。当你运行main包时,默认执行main函数。
 
- 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 run命令是许多go命令之一,你将使用它让Go代码完成工作。使用以下命令来获取帮助:$ 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. 访问pkg.go.dev并搜索quote包
- Locate and click the rsc.io/quotepackage in search results (if you seersc.io/quote/v3, ignore it for now). 在搜索结果中找到并点击rsc.io/quote包(如果看到rsc.io/quote/v3,暂时忽略它)
- In the Documentation section, under Index, note the list of functions you can call from your code. You'll use the Gofunction. 在Documenattion节的Index下,会看到可以在你的代码中调用的函数列表。你将使用Go()函数。
- At the top of this page, note that package quoteis included in thersc.io/quotemodule. 此页的顶部,会看到quote包被包括在rsc.io/quote模块中。
 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. 你可以使用pkg.go.dev网站来查找已发布的模块,这些模块包含可用于你自己的代码中的功能。包被发布到模块中——像rsc.io/quote——其他人可以使用它们。随着时间推移,模块由新版本改进,并且你可以升级你的代码来使用改进后的版本。
- In your Go code, import the rsc.io/quotepackage and add a call to itsGofunction. 在你的Go代码中,导入rsc.io/quote包并增加对其Go()函数的调用。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.  添加新的模块requirements和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将添加quote模块为一个requirements,同样一个名为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 
- 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. 请注意,你的代码调用Go()函数,打印了关于通信的明智消息。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. 当你执行go mod tidy命令,该命令会定位并下载rsc.io/quote模块,该模块包含了你导入的包。默认情况下,该命令下载最新版本——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.
随着快速介绍,你安装了Go并学习了一些基本知识。要通过另一个教程编写更多代码,请看创建一个Go模块。
 

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