Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save peterhellberg/20fb92998a9bc3a85583 to your computer and use it in GitHub Desktop.

Select an option

Save peterhellberg/20fb92998a9bc3a85583 to your computer and use it in GitHub Desktop.
Percentage Calculators in Go
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
if len(os.Args) == 3 {
v1 := getFloat(1)
v2 := getFloat(2)
fmt.Println(((v2 - v1) / ((v2 + v1) / 2.0)) * 100.0)
}
}
func getFloat(n int) float64 {
f, err := strconv.ParseFloat(os.Args[n], 64)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return f
}
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
if len(os.Args) == 3 {
v1 := getFloat(1)
v2 := getFloat(2)
fmt.Println((v1 / v2) * 100.0)
}
}
func getFloat(n int) float64 {
f, err := strconv.ParseFloat(os.Args[n], 64)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return f
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment