Golang 使用库函数获取整数的前驱

Golang 使用库函数获取整数的前驱

在本文中我们将讨论如何使用 Go 语言的库函数来获取整数的前驱。

一个整数的前驱被定义为在它之前的数字。例如,2 的前驱是 2 – 1 = 1。同样,-50 的前驱是 -51,依此类推。

语法

Syntax for Println() = fmt.Println(...interface{})

示例1:使用库函数获取整数的前一个数

步骤

  • 步骤1 - 导入fmt包。

  • 步骤2 - 开始主函数。

  • 步骤3 - 初始化一个int类型的变量并存储值。

  • 步骤4 - 实现查找前一个数的逻辑。

  • 步骤5 - 在屏幕上打印结果。

示例

// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY
// fmt package allows us to print anything on the screen.
package main
import "fmt"

// fmt package allows us to print anything on the screen.
// calling the main() function
func main() {
   fmt.Println("Golang program to find the predecessor of an integer number using library function")

   // initializing the number variable to store the required number.
   var number, predecessor int

   // assigning value to the number variable.
   number = 49

   // implementing the logic
   predecessor = number - 1

   // Print the result
   fmt.Println(" predecessor of", number, "is ", predecessor )
}

输出

Golang program to find the predecessor of an integer number using library function
predecessor of 49 is 48

代码描述

  • 首先,我们导入fmt包,它允许我们打印任何内容。

  • 然后,我们开始main()函数。

  • 之后,我们需要将数字赋给一个变量,并创建一个前身变量。

  • 然后创建逻辑以找到前一个整数。

  • 使用fmt.Println()函数在屏幕上打印结果。

示例2:使用库函数获取整数的前一个数,不带任何参数且没有返回值

步骤

  • 步骤1 - 导入fmt和其他包。

  • 步骤2 - 开始main()函数。

  • 步骤3 - 调用predecessor()函数。

  • 步骤4 - 开始predecessor()函数。

  • 步骤5 - 声明并初始化变量。

  • 步骤6 - 使用fmt.Printf()在控制台屏幕上打印结果。

示例

// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY FUNCTION
// fmt package allows us to print anything on the screen.
package main
import "fmt"

// function prototype
// GO program execution starts with the function main()
func main() {

   // function call
   predecessor()
}
func predecessor() {
   fmt.Println("Golang program to find the predecessor of an integer number using library function")
   // declare and initialize the variable
   var c int
   c = 4

   // print the result
   fmt.Println("predeccessor of the number",c,"=",c - 1)
}

输出

Golang program to find the predecessor of an integer number using library function
predeccessor of the number 4 = 3

代码描述

  • 首先,我们导入了允许我们打印任何内容的fmt包。

  • 现在让我们开始 主函数() 。GO程序执行从主函数()开始

  • 接下来我们调用函数 predecessor()

  • 现在我们开始函数 predecessor() 。声明并初始化整数变量c,要找到其前驱。

  • 在上述程序中,函数 predecessor() 执行计算,未传递任何参数给该函数。该函数的返回类型为void,因此无返回值。

  • 最终结果使用内置函数fmt.Printf()打印在控制台屏幕上。此函数在fmt包下定义,用于编写标准输出。

示例3:使用库函数在两个独立函数中使用参数和返回值获取整数的前驱

步骤

  • 步骤1 − 导入fmt包

  • 步骤2 − 创建和定义 predecessor() 函数。

  • 步骤3 − 开始主函数()

  • 步骤4 − 从用户获取值。

  • 步骤5 − 调用上面创建的 predecessor() 函数,并将用户输入的值传递给它。

  • 步骤6 − 在屏幕上打印结果。

示例

// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY
// fmt package allows us to print anything on the screen.
package main
import "fmt"
func predecessor(number int) int {
   var n int
   n = number - 1
   return n
}

// calling the main() function
func main() {
   fmt.Println("Golang program to find the predecessor of an integer number using library function")

   // declare the variables
   var number, previous int

   // initialize the number variable
   number = 66

   // implementing the logic.
   // function call
   previous = predecessor(number)

   // print the result
   fmt.Println(" predecessor of", number, "is ", previous )
}

输出

Golang program to find the predecessor of an integer number using library function
predecessor of 66 is 65

代码描述

  • 首先,我们导入了fmt包,它允许我们打印任何内容。

  • 然后创建并定义了predecessor函数,该函数将返回用户输入的整数的前一个值。

  • 然后我们开始了 main() 函数。

  • 我们声明了整数变量number和previous。用一个值初始化变量number以找到它的前一个值,并将结果存储在变量previous中。

  • 调用predecessor函数并存储答案。

  • 最后使用 fmt.Println() 函数在屏幕上打印结果。

结论

我们已经成功编译并执行了使用库函数找到整数的前一个值的Go语言程序,同时提供了示例。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程