3. Core Types
Activity 3.01: Sales Tax Calculator
Solution:
- Create a new folder and add a
main.go
file. - In
main.go
, add themain
package name to the top of the file:package main
- Now add the imports we'll use in this file:
import "fmt"
- Create a function that takes two floating-point arguments and returns a floating-point:
func salesTax(cost float64, taxRate float64) float64 {
- Multiply the two arguments together and return the result:
return cost * taxRate
- Close the function:
}
- Create the
main()
function:func main() {
- Declare a variable to be a floating-point:
taxTotal := .0
- Add
cake
to thetaxTotal
:// Cake taxTotal += salesTax(.99, .075)
- Add
milk
to thetaxTotal
:// Milk taxTotal += salesTax(2.75, .015)
- Add
butter
to thetaxTotal
:// Butter taxTotal += salesTax(.87, .02)
- Print the
taxTotal
to the console:// Total ...