How to do it...

The following steps cover how to write and run your application:

  1. From your Terminal/console application, create a new directory called ~/projects/go-programming-cookbook/chapter3/currency.
  2. Navigate to this directory.
  1. Run the following command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter3/currency

You should see a file called go.mod that contains the following:

module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter3/currency    
  1. Copy tests from ~/projects/go-programming-cookbook-original/chapter3/currency or use this as an exercise to write some of your own code!
  2. Create a file called dollars.go with the following content:
        package currency

import (
"errors"
"strconv"
"strings"
)

// ConvertStringDollarsToPennies takes a dollar amount
// as a string, that is, 1.00, 55.12,and so on and converts it
// into an int64
func ConvertStringDollarsToPennies(amount string) (int64,
error) {
// check if amount can convert to a valid float
_, err := strconv.ParseFloat(amount, 64)
if err != nil {
return 0, err
}

// split the value on "."
groups := strings.Split(amount, ".")

// if there is no . result will still be
// captured here
result := groups[0]

// base string
r := ""

// handle the data after the "."
if len(groups) == 2 {
if len(groups[1]) != 2 {
return 0, errors.New("invalid cents")
}
r = groups[1]
}

// pad with 0, this will be
// 2 0's if there was no .
for len(r) < 2 {
r += "0"
}

result += r

// convert it to an int
return strconv.ParseInt(result, 10, 64)
}
  1. Create a file called pennies.go with the following content:
        package currency

import (
"strconv"
)

// ConvertPenniesToDollarString takes a penny amount as
// an int64 and returns a dollar string representation
func ConvertPenniesToDollarString(amount int64) string {
// parse the pennies as a base 10 int
result := strconv.FormatInt(amount, 10)

// check if negative, will set it back later
negative := false
if result[0] == '-' {
result = result[1:]
negative = true
}

// left pad with 0 if we're passed in value < 100
for len(result) < 3 {
result = "0" + result
}
length := len(result)

// add in the decimal
result = result[0:length-2] + "." + result[length-2:]

// from the negative we stored earlier!
if negative {
result = "-" + result
}

return result
}
  1. Create a new directory named example and navigate to it.
  2. Create a file called main.go with the following content:
        package main

import (
"fmt"

"github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter3/currency"
)

func main() {
// start with our user input
// of fifteen dollars and 93 cents
userInput := "15.93"

pennies, err :=
currency.ConvertStringDollarsToPennies(userInput)
if err != nil {
panic(err)
}

fmt.Printf("User input converted to %d pennies ", pennies)

// adding 15 cents
pennies += 15

dollars := currency.ConvertPenniesToDollarString(pennies)

fmt.Printf("Added 15 cents, new values is %s dollars ",
dollars)
}
  1. Run go run main.go. You could also run the following:
$ go build
$ ./example

You should see the following output:

$ go run main.go
User input converted to 1593 pennies
Added 15 cents, new values is 16.08 dollars
  1. If you copied or wrote your own tests, go up one directory and run go test. Ensure that all tests pass.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.97.64