Skip to content

Instantly share code, notes, and snippets.

@marklap
Created May 1, 2014 15:03
Show Gist options
  • Save marklap/8db5d2c75ebf175b1bbf to your computer and use it in GitHub Desktop.
Save marklap/8db5d2c75ebf175b1bbf to your computer and use it in GitHub Desktop.
Just toying around with Go
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