Golang 如何添加两个数字

Golang 如何添加两个数字

在本教程中,我们将讨论如何在Golang中添加两个数字。我们将讨论两种方法:第一种是在函数内部添加两个数字,第二种是创建一个不同的函数来完成添加操作。

在函数内部添加两个数字

步骤

  • 第一步 - 定义要相加的变量。

  • 第二步 - 初始化变量。

  • 第三步 - 相加两个数字,并将结果存储在第三个变量中。

  • 第四步 - 在添加两个数字之后打印结果。

示例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

在这个示例中,我们将调用主函数外的一个函数来添加两个整数。

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

在这个示例中,我们将通过调用一个位于主函数之外的函数来添加两个浮点数。

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的更多信息,您可以探索 这些 教程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程