如何在 Golang 中新增兩個數字?
在本教程中,我們將討論在 Golang 中新增兩個數字。我們將介紹兩種方法,首先是在函式內新增兩個數字,其次是建立不同的函式。
在函式內新增兩個數字
演算法
步驟 1 - 定義我們想要新增的變數。
步驟 2 - 初始化變數。
步驟 3 - 將兩個數字相加並存儲到第三個變數中。
步驟 4 - 列印新增兩個數字後的結果。
示例 1
在這個例子中,我們將新增函式內的兩個整數。
package main // fmt package provides the function to print anything import "fmt" func main() { // define the variables we want to add var number1, number2, number3 int // initializing the variables number1 = 99 number2 = 81 // adding the numbers number3 = number1 + number2 // printing the results fmt.Println("The addition of ", number1, " and ", number2, " is \n ", number3, "\n(Addition of two integers within the function)") }
在上面的程式碼中,我們首先聲明瞭兩個整型變數,然後初始化了這些變數。在下一步中,我們將兩個值相加並將它們儲存到第三個整型變數中。最後,我們在最後一步列印加法的結果。
輸出
The addition of 99 and 81 is 180 (Addition of two integers within the function)
示例 2
在這個例子中,我們將新增函式內的兩個浮點數。
package main // fmt package provides the function to print anything import "fmt" func main() { // define the float32 variables we want to add var number1, number2, number3 float32 // initializing the variables number1 = 74 number2 = 22 // adding the float32 numbers number3 = number1 + number2 // printing the results fmt.Println("The addition of ", number1, " and ", number2, " is \n", number3, "\n(Adding two float numbers within the function)") }
在上面的程式碼中,我們首先聲明瞭兩個 float32 變數,然後初始化了這些變數。在下一步中,我們將兩個值相加並將它們儲存到第三個 float32 變數中。最後,我們在最後一步列印加法的結果。
輸出
The addition of 74 and 22 is 96 (Adding two float numbers within the function)
在函式外部新增兩個數字
演算法
步驟 1 - 定義我們想要新增的變數。
步驟 2 - 初始化變數。
步驟 3 - 透過呼叫addNumber()函式新增兩個數字並將它們儲存到第三個變數中。
步驟 4 - 列印結果
示例 1
在這個例子中,我們將透過在 main 函式外部呼叫一個函式來新增兩個整數。
package main // fmt package provides the function to print anything import "fmt" // function to add the two integer numbers func addNumber(number1, number2 int) int { return number1 + number2 } func main() { // define the integer variables we want to add var number1, number2, number3 int // initializing the variables number1 = 18 number2 = 9 // calling the function and storing the result number3 = addNumber(number1, number2) // printing the results fmt.Println("The addition of ", number1, " and ", number2, " is \n", number3, "\n(adding two integers outside the function)") }
在上面的程式碼中,我們首先聲明瞭兩個整型變數並在下一步中初始化它們。然後我們呼叫我們在函式外部建立的addNumber()函式,並將其儲存在第三個整型變數中,最後列印加法後的結果。
輸出
The addition of 18 and 9 is 27 (adding two integers outside the function)
示例 2
在這個例子中,我們將透過在 main 函式外部呼叫一個函式來新增兩個浮點數。
package main // fmt package provides the function to print anything import "fmt" // function to add the two float32 numbers func addNumber(number1, number2 float32) float32 { return number1 + number2 } func main() { // define the float32 variables we want to add var number1, number2, number3 float32 // initializing the variables number1 = 2.333 number2 = 4.87 // calling the function and storing the result number3 = addNumber(number1, number2) // printing the results fmt.Println("The addition of ", number1, " and ", number2, " is \n", number3, "\n(adding two float numbers outside the function)") }
在上面的程式碼中,我們首先聲明瞭兩個整型變數並在下一步中初始化它們。然後我們呼叫我們在函式外部建立的addNumber()函式,並將其儲存在第三個整型變數中,最後列印加法後的結果。
輸出
The addition of 2.333 and 4.87 is 7.203 (adding two float numbers outside the function)
這就是關於新增兩個數字的所有內容。此外,如果我們談論哪種方式更好,比如在函式內新增或在函式外部新增,那麼在函式外部新增的方法更好,這樣我們就可以在不同的地方使用該函式。要了解有關 Golang 的更多資訊,您可以瀏覽這些教程。