find the area of a trapezoid in go

To find the area of a trapezoid in go programming language, the formula to be used is:

main.go
area = (b1+b2)/2 * h
21 chars
2 lines

where b1 and b2 are the lengths of the parallel bases, and h is the height (the perpendicular distance between the bases).

Here is a go function that takes in the values of b1, b2, and h, and returns the area of the trapezoid:

main.go
func trapezoidArea(b1, b2, h float64) float64 {
    area := (b1 + b2) / 2 * h
    return area
}
96 chars
5 lines

You can call this function with the appropriate values and it will return the area of the trapezoid.

Here is an example usage:

main.go
package main

import "fmt"

func main() {
    b1 := 10.0
    b2 := 12.0
    h := 5.0
    area := trapezoidArea(b1, b2, h)
    fmt.Println("Area of trapezoid:", area)
}
168 chars
12 lines

gistlibby LogSnag