Golang 如何进行两数相加

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外的一个函数来增加两个float。

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)

这都是关于两个数字的加法。另外,如果我们谈论哪种方法更好,比如在函数内加或者在函数外加,在函数外加的方法更好,这样我们就可以在不同的地方使用函数。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程