Skip to content

Instantly share code, notes, and snippets.

@pgburt
Last active March 1, 2016 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgburt/d8f863e5009abf5e81a9 to your computer and use it in GitHub Desktop.
Save pgburt/d8f863e5009abf5e81a9 to your computer and use it in GitHub Desktop.
Sending results to an MQ.
package main
import (
"fmt"
"math"
"math/big"
"encoding/json"
"github.com/iron-io/iron_go3/mq"
"github.com/iron-io/iron_go/worker"
)
type RangeToCalc struct {
Begin int
End int
}
type CalcResults struct {
Begin int
End int
Result string
}
var mqName string = "pi-map-first"
func CalcNumerator(num int) int64 {
odd := math.Mod(float64(num), 2)
if (odd == 1) {
return -1
}
return 1 // otherwise, it's even
}
func QueueItUp(sum string, r *RangeToCalc) {
msg_struct := CalcResults {
r.Begin,
r.End,
sum,
}
jsons, err := json.MarshalIndent(msg_struct, "", " ")
if err != nil {
fmt.Println("Error: ", err)
}
s := string(jsons)
q := mq.New(mqName);
id, err := q.PushString(s)
if err != nil {
fmt.Println("Error: ", err)
}
fmt.Println("We did the thing! Its Id is: ", id)
}
func main() {
sum := big.NewRat(0, 1)
worker.ParseFlags()
r := &RangeToCalc{}
worker.PayloadFromJSON(r)
for i := r.Begin; i <= r.End; i++ {
leibnizNum := CalcNumerator(i)
leibnizDen := int64(2 * i + 1)
leibnizRat := big.NewRat(leibnizNum, leibnizDen)
sum.Add(sum, leibnizRat)
}
QueueItUp(sum.String(), r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment