企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 创建一个Go模块 这是教程的第一部分,介绍了Go语言的一些基本特性。如果你刚开始接触Go,一定要看看[入门](https://golang.org/doc/tutorial/getting-started.html)教程,其中介绍了`go`命令、Go模块和非常简单的Go代码。 在本教程中,你将创建两个模块。第一个是一个库,它可以被其他库或应用程序导入。第二个是一个调用程序,它将使用第一个模块。 本教程的顺序包括六个简短的主题,每个主题说明了语言的不同部分。 1. 创建一个模块 -- 编写一个小模块,里面的功能可以从另一个模块调用。 2. 从另一个模块调用你的代码](https://golang.org/doc/tutorial/call-module-code.html) --导入并使用你的新模块。 3. [返回和处理错误](https://golang.org/doc/tutorial/handle-errors.html) -- 添加简单的错误处理。 4. [返回一个随机的问候语](https://golang.org/doc/tutorial/random-greeting.html) -- 处理片中的数据(Go的动态大小数组)。 5. [返回多人的问候语](https://golang.org/doc/tutorial/greetings-multiple-people.html) -- 在地图中存储键/值对。 6. 添加测试](https://golang.org/doc/tutorial/add-a-test.html) -- 使用 Go 的内置单元测试功能来测试您的代码。 7. [编译并安装应用程序](https://golang.org/doc/tutorial/compile-install.html) -- 在本地编译并安装你的代码。 通过www.DeepL.com/Translator(免费版)翻译 **注:**其他教程见【教程】(https://golang.org/doc/tutorial/index.html)。 ## 先决条件 - **有一定的编程经验.**这里的代码非常简单,但了解一些关于函数,循环和数组的知识是有帮助的。 - **一个编辑代码的工具.**任何你有的文本编辑器都可以工作。大多数文本编辑器都对Go有很好的支持。最流行的是 VSCode(免费)、GoLand(付费)和 Vim(免费)。 - **命令终端.** Go在Linux和Mac上使用任何终端都可以很好地工作,在Windows上使用PowerShell或cmd也可以。 ## 启动一个别人可以使用的模块 首先创建一个[Go模块](https://golang.org/doc/code.html#Organization)。在一个模块中,你为一组离散而有用的功能收集了一个或多个相关的包。例如,你可以用具有做财务分析功能的包来创建一个模块,以便其他编写财务应用程序的人可以使用你的工作。 围棋代码被归为包,包被归为模块。你的包的模块指定了Go运行代码所需要的上下文,包括代码所编写的Go版本和它所需要的其他模块的集合。 当你在模块中添加或改进功能时,你会发布模块的新版本。开发人员在编写调用您的模块中的功能的代码时,可以导入模块的更新包,并在投入生产使用之前用新版本进行测试。 1. 打开一个命令提示符,然后 ``` 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. This is where you'll write your module 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 to create a go.mod file. Run the `go mod init` command, giving it the path of the module your code will be in. Here, use `example.com/greetings` for the module path -- in production code, this would be the URL from which your module can be downloaded. ``` $ 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 that identifies your code as a module that might be used from other code. The file you just created includes only the name of your module and the Go version your code supports. But as you add dependencies -- meaning packages from other modules -- the go.mod file will list the specific module versions to use. 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`, and 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](https://tour.golang.org/basics/3). - 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](https://golang.org/doc/tutorial/call-module-code.html), you'll call this function from another module.