Skip to content

Instantly share code, notes, and snippets.

@mikezter
Forked from anonymous/copy.go
Created September 21, 2012 23:22
Show Gist options
  • Save mikezter/3764479 to your computer and use it in GitHub Desktop.
Save mikezter/3764479 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"io/ioutil"
"math"
"math/rand"
"os"
"strings"
)
// Contents returns the file's contents as a string.
func Contents(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
return "", err
}
defer f.Close() // f.Close will run when we're finished.
var result []byte
buf := make([]byte, 100)
for {
n, err := f.Read(buf[0:])
result = append(result, buf[0:n]...) // append is discussed later.
if err != nil {
if err == io.EOF {
break
}
return "", err // f will be closed if we return here.
}
}
return string(result), nil // f will be closed if we return here.
}
func isDigit(i byte) bool {
return true
}
func nextInt(b []byte, i int) (int, int) {
for ; i < len(b) && !isDigit(b[i]); i++ {
}
x := 0
for ; i < len(b) && isDigit(b[i]); i++ {
x = x*10 + int(b[i]) - '0'
}
return x, i
}
func copy_go() {
fmt.Println("Hi!")
fmt.Println(global.String())
content, err := Contents(" copy.go")
fmt.Println(err)
fmt.Println(content)
a := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(nextInt(a, 3))
fmt.Println(WordCount("a as a a her his sa qwwe her"))
thePower := 10.0
fmt.Println(Sqrt(thePower))
fmt.Println(math.Sqrt(thePower))
//CopyDigits("copy.go")
//digits := AppendDigits("copy.go")
//fmt.Println(digits)
return
}
func Sqrt(x float64) float64 {
z := 1.0
for i := 0; i < 10; i++ {
z = z - (z*z-x)/(2*z)
}
return z
}
func WordCount(s string) map[string]int {
words := strings.Fields(s)
res := make(map[string]int)
for _, word := range words {
if res[word] > 0 {
res[word] += 1
} else {
res[word] = 1
}
}
return res
}
type Handle int64
func (that Handle) String() string {
return "This is a handle object that cannot be represented as String."
}
var global Handle
func Filter(s []byte, fn func(byte) bool) []byte {
var p []byte // == nil
for _, i := range s {
if fn(i) {
p = append(p, i)
}
}
return p
}
func CopyDigits(filename string) []byte {
b, _ := ioutil.ReadFile(filename)
c := make([]byte, len(b))
copy(c, b)
return c
}
func Possibly(i byte) bool {
a := rand.Intn(255)
return (a < 128)
}
func AppendDigits(filename string) []byte {
b, _ := ioutil.ReadFile(filename)
c := Filter(b, Possibly)
return c
}
package main
import (
"encoding/json"
"fmt"
)
type Location struct {
Id string
Contingents []int
}
type Contingents []float64
type Cluster struct {
Cpqa float64
Dqw float64
}
type Arg struct {
Locations []Location
Contingents
Clusters []Cluster
}
type Job struct {
Class string
Args []Arg
}
func doWork(locations []Location, contingents Contingents, clusters []Cluster) error {
fmt.Printf("From %v \n", locations, contingents, clusters)
return nil
}
func main() {
data := data()
job := getJob(data)
args := job.Args[0]
doWork(args.Locations, args.Contingents, args.Clusters)
}
func getJob(data []byte) Job {
var job Job
err := json.Unmarshal(data, &job)
if err != nil {
fmt.Print("err ")
fmt.Println(err)
}
fmt.Print("unmarshal ")
fmt.Println(job)
return job
}
func data() []byte {
data := []byte(`
{ "class":"MyClass",
"args":[{
"locations":[
{ "id":"1234", "contingents":[2, 3]},
{ "id":"2afe", "contingents":[1, 0, 4]},
{ "id":"9283", "contingents":[2, 1, 4]}
],
"contingents":[100,200,300,400,500,199.4],
"clusters":[{"cpqa":23.4,"dqw":29.7}]
}] }
`)
fmt.Print("data ")
fmt.Println(string(data))
return data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment