p1.1
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println(os.Args[0])
}
p1.3
package main
import (
"fmt"
"strings"
"time"
)
func main() {
ss := []string{"a", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"}
start := time.Now().Nanosecond()
s, sep := "", ""
for _, arg := range ss {
s += sep + arg
sep = " "
}
fmt.Println(s)
end := time.Now().Nanosecond()
fmt.Println(end - start)
start = time.Now().Nanosecond()
fmt.Println(strings.Join(ss, " "))
end = time.Now().Nanosecond()
fmt.Println(end - start)
}
p1.7
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func main() {
for _, url := range os.Args[1:] {
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
os.Exit(1)
}
//b, err := ioutil.ReadAll(resp.Body)
_, err = io.Copy(os.Stdout, resp.Body)
resp.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
os.Exit(1)
}
//fmt.Printf("%s", b)
}
}
p1.8-1.9
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
)
func main() {
for _, url := range os.Args[1:] {
if !strings.HasPrefix(url, "http://") {
url = "http://" + url
}
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
os.Exit(1)
}
//b, err := ioutil.ReadAll(resp.Body)
_, err = io.Copy(os.Stdout, resp.Body)
resp.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
os.Exit(1)
}
fmt.Printf("%s", resp.Status)
}
}
p1.12
package main
import (
"image"
"image/color"
"image/gif"
"io"
"log"
"math"
"math/rand"
"net/http"
)
var palette = []color.Color{color.White, color.Black}
const (
whiteIndex = 0 // first color in palette
blackIndex = 1 // next color in palette
)
func main() {
handler := func(w http.ResponseWriter, r *http.Request) {
lissajous(w)
}
http.HandleFunc("/", handler) // each request calls handler
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
func lissajous(out io.Writer) {
const (
cycles = 5 // number of complete x oscillator revolutions
res = 0.001 // angular resolution
size = 100 // image canvas covers [-size..+size]
nframes = 64 // number of animation frames
delay = 8 // delay between frames in 10ms units
)
freq := rand.Float64() * 3.0 // relative frequency of y oscillator
anim := gif.GIF{LoopCount: nframes}
phase := 0.0 // phase difference
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
blackIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
}
//!-
p2.1
conv.go
package tempconv
// CToF converts a Celsius temperature to Fahrenheit.
func CToF(c Celsius) Fahrenheit { return Fahrenheit(c*9/5 + 32) }
// FToC converts a Fahrenheit temperature to Celsius.
func FToC(f Fahrenheit) Celsius { return Celsius((f - 32) * 5 / 9) }
func KToC(k Kelvins) Celsius { return Celsius((k * (-273.15))) }
func CToK(c Celsius) Kelvins { return Kelvins(c / (-273.15)) }
tempconv.go
package tempconv
import "fmt"
type Celsius float64
type Fahrenheit float64
type Kelvins float64
const (
AbsoluteZeroC Celsius = -273.15
FreezingC Celsius = 0
BoilingC Celsius = 100
)
func (c Celsius) String() string { return fmt.Sprintf("%g°C", c) }
func (f Fahrenheit) String() string { return fmt.Sprintf("%g°F", f) }
func (k Kelvins) String() string { return fmt.Sprintf("%g°K", k) }
p2.2
package main
import (
tempconv "ch2/p2.1"
"fmt"
"os"
"strconv"
)
func main() {
for _, arg := range os.Args[1:] {
t, err := strconv.ParseFloat(arg, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "cf: %v\n", err)
os.Exit(1)
}
f := tempconv.Fahrenheit(t)
c := tempconv.Celsius(t)
k := tempconv.Kelvins(t)
fmt.Printf("%s = %s, %s = %s, %s = %s\n",
f, tempconv.FToC(f), c, tempconv.CToF(c), k, tempconv.KToC(k))
}
}