如何在 Golang 中添加两个数字?


在本教程中,我们将讨论在 Golang 中添加两个数字。我们将介绍两种方法,首先是在函数内添加两个数字,其次是创建不同的函数。

在函数内添加两个数字

算法

  • 步骤 1 - 定义我们想要添加的变量。

  • 步骤 2 - 初始化变量。

  • 步骤 3 - 将两个数字相加并存储到第三个变量中。

  • 步骤 4 - 打印添加两个数字后的结果。

示例 1

在这个例子中,我们将添加函数内的两个整数。

Open Compiler
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

在这个例子中,我们将添加函数内的两个浮点数。

Open Compiler
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 函数外部调用一个函数来添加两个整数。

Open Compiler
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 函数外部调用一个函数来添加两个浮点数。

Open Compiler
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 的更多信息,您可以浏览这些教程。

更新于: 2022年8月26日

5K+ 次查看

开启你的 职业生涯

通过完成课程获得认证

开始
广告