<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 創建一個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.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看