Skip to content

Commit f0eb3a5

Browse files
author
Lukas Werner
committed
finsih most of the chapter
1 parent 6e92834 commit f0eb3a5

File tree

7 files changed

+114
-2
lines changed

7 files changed

+114
-2
lines changed

ch3/basename1/basename.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println(basename("a/b/c.go"))
7+
}
8+
9+
func basename(s string) string {
10+
for i := len(s) - 1; i >= 0; i-- {
11+
if s[i] == '/' {
12+
s = s[i+1:]
13+
break
14+
}
15+
}
16+
for i := len(s) - 1; i >= 0; i-- {
17+
if s[i] == '.' {
18+
s = s[:i]
19+
break
20+
}
21+
}
22+
return s
23+
}

ch3/basename2/basename.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func main() {
9+
fmt.Println(basename("a/b/c.go"))
10+
}
11+
12+
func basename(s string) string {
13+
slash := strings.LastIndex(s, "/")
14+
s = s[slash+1:]
15+
if dot := strings.LastIndex(s, "."); dot >= 0 {
16+
s = s[:dot]
17+
}
18+
return s
19+
}

ch3/comma/comma.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
fmt.Println(comma("1234567890"))
9+
}
10+
11+
func comma(s string) string {
12+
n := len(s)
13+
if n <= 3 {
14+
return s
15+
}
16+
return comma(s[:n-3]) + "," + s[n-3:]
17+
}

ch3/mandelbrot/mandelbrot.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,45 @@
22

33
package main
44

5-
import "image"
5+
import (
6+
"image"
7+
"image/color"
8+
"image/png"
9+
"math/cmplx"
10+
"os"
11+
)
612

713
func main() {
14+
f, err := os.Create("out.png")
15+
if err != nil {
16+
os.Exit(1)
17+
}
818
const (
919
xmin, ymin, xmax, ymax = -2, -2, +2, +2
1020
width, height = 1024, 1024
1121
)
1222
img := image.NewRGBA(image.Rect(0, 0, width, height))
1323
for py := 0; py < height; py++ {
14-
y := float64(py) / height * (ymax)
24+
y := float64(py)/height*(ymax-ymin) + ymin
25+
for px := 0; px < width; px++ {
26+
x := float64(px)/width*(xmax-xmin) + xmin
27+
z := complex(x, y)
28+
img.Set(px, py, mandelbrot(z))
29+
}
30+
}
31+
png.Encode(f, img)
32+
f.Close()
33+
}
34+
35+
func mandelbrot(z complex128) color.Color {
36+
const iterations = 200
37+
const contrast = 15
38+
var v complex128
39+
for n := uint8(0); n < iterations; n++ {
40+
v = v*v + z
41+
if cmplx.Abs(v) > 2 {
42+
return color.Gray{255 - contrast*n}
43+
}
1544
}
45+
return color.Black
1646
}

ch3/mandelbrot/out.png

88.2 KB
Loading

ch3/printints/printints.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
fmt.Println(intsToString([]int{1, 2, 3}))
10+
}
11+
12+
func intsToString(values []int) string {
13+
var buf bytes.Buffer
14+
buf.WriteByte('[')
15+
for i, v := range values {
16+
if i > 0 {
17+
buf.WriteString(", ")
18+
}
19+
fmt.Fprintf(&buf, "%d", v)
20+
}
21+
buf.WriteByte(']')
22+
return buf.String()
23+
}

ch3/test.svg

-3.23 MB
Binary file not shown.

0 commit comments

Comments
 (0)