Created
May 1, 2014 15:03
-
-
Save marklap/8db5d2c75ebf175b1bbf to your computer and use it in GitHub Desktop.
Just toying around with Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
const POUNDS_IN_GRAM float64 = 0.00220462 | |
const OUNCES_IN_GRAM float64 = 16.0 | |
type Thinger interface { | |
Name() string | |
SetGrams(float64) | |
WeightGrams() float64 | |
WeightPounds() float64 | |
WeightPoundsOunces() (int64, float64) | |
} | |
type Thing struct { | |
name string | |
grams float64 | |
} | |
func NewThing(name string, grams float64) *Thing { | |
return &Thing{fmt.Sprintf("Thing%s", name), grams} | |
} | |
func (self *Thing) SetGrams(grams float64) { | |
self.grams = grams | |
} | |
func (self Thing) Name() string { | |
return self.name | |
} | |
func (self Thing) WeightGrams() float64 { | |
return self.grams | |
} | |
func (self Thing) WeightPounds() float64 { | |
return self.grams * POUNDS_IN_GRAM | |
} | |
func (self Thing) WeightPoundsOunces() (int64, float64) { | |
lbs_fraction := self.WeightPounds() | |
lbs := math.Floor(lbs_fraction) | |
ozs := (lbs_fraction - lbs) * OUNCES_IN_GRAM | |
return int64(lbs), ozs | |
} | |
func main() { | |
var T Thinger | |
t := NewThing("Mark", 2450.0) | |
T = t | |
lbs, ozs := T.WeightPoundsOunces() | |
fmt.Println(T.Name(), "Weight in Grams =", T.WeightGrams()) | |
fmt.Println(T.Name(), "Weight in Pounds =", T.WeightPounds()) | |
fmt.Println(T.Name(), "Weight in Pounds and Ounces =", lbs, "lbs", ozs, "ozs") | |
T.SetGrams(2091020.0) | |
lbs, ozs = T.WeightPoundsOunces() | |
fmt.Println(T.Name(), "Weight in Grams =", T.WeightGrams()) | |
fmt.Println(T.Name(), "Weight in Pounds =", T.WeightPounds()) | |
fmt.Println(T.Name(), "Weight in Pounds and Ounces =", lbs, "lbs", ozs, "ozs") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment