# jwang/I-O-Bootcamp---Go

### Subversion checkout URL

You can clone with
or
.

google io bootcamp go session exercises and notes

commit 31de1b9bfd70ce2e2959e1ef0a0738474c31f373 0 parents
authored
Showing with 209 additions and 0 deletions.
1. +65 −0 Notes.go
2. +30 −0 exercises.go
3. +7 −0 hello.go
4. +76 −0 image.go
5. +31 −0 web.go
65 Notes.go
 @@ -0,0 +1,65 @@ +package main + +import fmt "fmt" + +type Foo int + +type Vertex struct { + X int + Y int +} + +func add(x int, y int) int { + return x + y +} + + +func main() { + fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n") + fmt.Println(add(1, 3)) + + var f Foo = 1 + g := Foo(2) + fmt.Println(g) + fmt.Println(f) + + // no pointer arithmetic + + var v Vertex + q := &v + + // Maps = HashMap-like + // map[string]int + //var m map[string]int + //m = make(map[string]int) + // or just do + m := make(map[string]int) + + m["Bootcamp"] = 2011 + i := m["Bootcamp"] + + + var n = map[string]bool { + "true": true, + "false": false, + } + + type Cache map[string]*Entry + + o := Cache{"x": &Entry{1}, "y": &Entry{2}} + + // Slices - like smart arrays + // []int + // []*Entry + + var x []*Entry // len(x) == 0 + y := make([]*Entry, 10) // len(y) == 10 + z := make([]*Entry, 0, 10) // len(z) == 0, cap(z) == 10 - capacity + + y[0] = 10 + a := y[0] // == 10 + + b := z[0] // index overflow, 0 >= len(z) + + +}
30 exercises.go
 @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "flag" + "strconv" + "math" + ) + +func Sqrt(x float64) float64 { + return math.Sqrt(x) +} + +func main() { + fmt.Println("Hello, world") + fmt.Println(Sqrt(4)) + + for i, v := range flag.Args() { + fmt.Println(i, v) + f, error := strconv.Atof64(v) + if error != nil { + fmt.Println("err: " + error.String()) + return + } + fmt.Println(Sqrt(f)) + + } + + +}
7 hello.go
 @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Printf("hello, world\n") +}
76 image.go
 @@ -0,0 +1,76 @@ +package main + +import ( + "http" + "image" + "image/png" +) + +type Image interface { + // ColorModel returns the Image's ColorModel. + ColorModel() ColorModel + // Bounds returns the domain for which At can return non-zero color. + Bounds() Rectangle + // At returns the color of the pixel at (x, y). + At(x, y int) Color +} + +var m image.Image + +func img(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-type", "image/png") + png.Encode(w, m) +} + +func main() { + rgba := image.NewRGBA(300, 300) + for x := 0; x < 300; x++ { + for y := 0; y < 300; y++ { + rgba.Set(x, y, image.RGBAColor{0, 0, 0, 255}) + } + } + m = rgba + http.HandleFunc("/", img) + http.ListenAndServe(":8080", nil) +} + +/* - Final code +package main + +import ( + "http" + "image" + "image/png" +) + +var m image.Image + +func img(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-type", "image/png") + png.Encode(w, m) +} + +type MyImage struct { + width, height int +} + +func (m MyImage) ColorModel() image.ColorModel { + return image.RGBAColorModel +} + +func (m MyImage) Bounds() image.Rectangle { + return image.Rect(0, 0, m.width, m.height) +} + +func (m MyImage) At(x, y int) image.Color { + c := uint8(x*x + y*y) + return image.RGBAColor{c, c, c, 255} +} + +func main() { + m = MyImage{300, 300} + http.HandleFunc("/", img) + http.ListenAndServe(":8080", nil) +} + +*/
31 web.go
 @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "http" +) + +type String string +func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, s) +} + +type Struct struct { + Greeting string + Punct string + Who string +} +func (s Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, s) +} + +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "Hello, ", r.FormValue("name")) +} + +func main() { + http.HandleFunc("/", handler) + http.Handle("/string", String("I'm a frayed knot.")) + http.Handle("/struct", Struct{"Hello", ":", "Gophers!"}) + http.ListenAndServe(":8080", nil) +}