Golang 如何查找平行四边形的面积
在本教程中,我们将看到Golang程序来寻找平行四边形的面积。面积是指任何封闭图形所覆盖的总空间。

公式
Area of the Parallelogram - base * height
b - length of the base of a Parallelogram
h - length of the height of a Parallelogram
For example, the length of the base of a Parallelogram is 6 cm and the length of the height of the parallelogram is 4 cm so the Area of a Parallelogram is −
b = 6 cm
h = 4 cm
Area = base * height
= 6 * 4
= 24 cm^2
在函数中寻找平行四边形的面积
算法
第1步 --声明底长、高长和float64数据类型的面积的变量。
第2步 --初始化变量。
第3步 – 使用函数中的上述公式求面积。
第4步 – 打印结果。
Time Complexity:
O(1)
Space Complexity:
O(1)
例1
在这个例子中,我们要在函数中找出平行四边形的面积。
package main
// fmt package provides the function to print anything
import (
"fmt"
)
func main() {
// declaring the floating variables using the var keyword for
// storing the length of the base of the Parallelogram,
// length of the height of the Parallelogram and also
// a variable area to store Area
var base, height, Area float64
fmt.Println("Program to find the Area of a Parallelogram.")
// initializing the length of the base of a Parallelogram
base = 6
// initializing the length of the height of a Parallelogram
height = 4
// finding the Area of a Parallelogram
Area = height * base
// printing the result
fmt.Println("The Area of a Parallelogram with base =", base,"and height =", height, "is", Area, "cm * cm.")
fmt.Println("(Finding the Area of a Parallelogram within the function)")
}
输出
Program to find the Area of a Parallelogram.
The Area of a Parallelogram with base = 6 and height = 4 is 24 cm * cm.
(Finding the Area of a Parallelogram within the function)
寻找平行四边形的面积的独立函数
算法
第1步 - 声明底长、高长和float64数据类型的面积的变量。
第2步 --初始化变量。
第3步 – 用平行四边形的底长和高长调用函数,并存储函数返回的面积。
第4步 - 打印结果。
例2
在这个例子中,我们将通过定义单独的函数来求平行四边形的面积。
package main
// fmt package provides the function to print anything
import (
"fmt"
)
// in this line we have declared the function that have float64
// type parameter and float64 type returntype
func areaOfParallelogram(base, height float64) float64 {
// returning the area by applying the formula
return base * height
}
func main() {
// declaring the floating variables using the var keyword for
// storing the length of the base of the Parallelogram,
// length of the height of the Parallelogram and also
// a variable area to store Area
var base, height, Area float64
fmt.Println("Program to find the Area of a Parallelogram.")
// initializing the length of the base of a Parallelogram
base = 6
// initializing the length of the height of a Parallelogram
height = 4
// finding the Area of a Parallelogram by calling the
// function with the respective parameters
Area = areaOfParallelogram(base, height)
// printing the result
fmt.Println("The Area of a Parallelogram with base =", base,"and height =", height, "is", Area, "cm * cm.")
fmt.Println("(Finding the Area of a Parallelogram in the separate function)")
}
输出
Program to find the Area of a Parallelogram.
The Area of a Parallelogram with base = 6 and height = 4 is 24 cm * cm.
(Finding the Area of a Parallelogram in the separate function)
结论
以上是在Golang中寻找平行四边形面积的两种方法。第二种方法在模块化和代码复用性方面要好得多,因为我们可以在项目的任何地方调用该函数。
极客教程