mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-12-20 12:44:20 +08:00
ch8: fix code format
This commit is contained in:
172
ch8/ch8-05.md
172
ch8/ch8-05.md
@@ -2,9 +2,10 @@
|
||||
|
||||
本節中,我們會探索一些用來在併行時循環迭代的常見併發模型。我們會探究從全尺寸圖片生成一些縮略圖的問題。gopl.io/ch8/thumbnail包提供了ImageFile函數來幫我們拉伸圖片。我們不會説明這個函數的實現,隻需要從gopl.io下載它。
|
||||
|
||||
<u><i>gopl.io/ch8/thumbnail</i></u>
|
||||
```go
|
||||
gopl.io/ch8/thumbnail
|
||||
package thumbnail
|
||||
|
||||
// ImageFile reads an image from infile and writes
|
||||
// a thumbnail-size version of it in the same directory.
|
||||
// It returns the generated file name, e.g., "foo.thumb.jpg".
|
||||
@@ -13,15 +14,15 @@ func ImageFile(infile string) (string, error)
|
||||
|
||||
下面的程序會循環迭代一些圖片文件名,併爲每一張圖片生成一個縮略圖:
|
||||
|
||||
<u><i>gopl.io/ch8/thumbnail</i></u>
|
||||
```go
|
||||
gopl.io/ch8/thumbnail
|
||||
// makeThumbnails makes thumbnails of the specified files.
|
||||
func makeThumbnails(filenames []string) {
|
||||
for _, f := range filenames {
|
||||
if _, err := thumbnail.ImageFile(f); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
for _, f := range filenames {
|
||||
if _, err := thumbnail.ImageFile(f); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -32,9 +33,9 @@ func makeThumbnails(filenames []string) {
|
||||
```go
|
||||
// NOTE: incorrect!
|
||||
func makeThumbnails2(filenames []string) {
|
||||
for _, f := range filenames {
|
||||
go thumbnail.ImageFile(f) // NOTE: ignoring errors
|
||||
}
|
||||
for _, f := range filenames {
|
||||
go thumbnail.ImageFile(f) // NOTE: ignoring errors
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -45,29 +46,28 @@ func makeThumbnails2(filenames []string) {
|
||||
```go
|
||||
// makeThumbnails3 makes thumbnails of the specified files in parallel.
|
||||
func makeThumbnails3(filenames []string) {
|
||||
ch := make(chan struct{})
|
||||
for _, f := range filenames {
|
||||
go func(f string) {
|
||||
thumbnail.ImageFile(f) // NOTE: ignoring errors
|
||||
ch <- struct{}{}
|
||||
}(f)
|
||||
}
|
||||
// Wait for goroutines to complete.
|
||||
for range filenames {
|
||||
<-ch
|
||||
}
|
||||
ch := make(chan struct{})
|
||||
for _, f := range filenames {
|
||||
go func(f string) {
|
||||
thumbnail.ImageFile(f) // NOTE: ignoring errors
|
||||
ch <- struct{}{}
|
||||
}(f)
|
||||
}
|
||||
// Wait for goroutines to complete.
|
||||
for range filenames {
|
||||
<-ch
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
註意我們將f的值作爲一個顯式的變量傳給了函數,而不是在循環的閉包中聲明:
|
||||
|
||||
|
||||
```go
|
||||
for _, f := range filenames {
|
||||
go func() {
|
||||
thumbnail.ImageFile(f) // NOTE: incorrect!
|
||||
// ...
|
||||
}()
|
||||
go func() {
|
||||
thumbnail.ImageFile(f) // NOTE: incorrect!
|
||||
// ...
|
||||
}()
|
||||
}
|
||||
```
|
||||
|
||||
@@ -79,22 +79,22 @@ for _, f := range filenames {
|
||||
// makeThumbnails4 makes thumbnails for the specified files in parallel.
|
||||
// It returns an error if any step failed.
|
||||
func makeThumbnails4(filenames []string) error {
|
||||
errors := make(chan error)
|
||||
errors := make(chan error)
|
||||
|
||||
for _, f := range filenames {
|
||||
go func(f string) {
|
||||
_, err := thumbnail.ImageFile(f)
|
||||
errors <- err
|
||||
}(f)}
|
||||
}
|
||||
for _, f := range filenames {
|
||||
go func(f string) {
|
||||
_, err := thumbnail.ImageFile(f)
|
||||
errors <- err
|
||||
}(f)}
|
||||
}
|
||||
|
||||
for range filenames {
|
||||
if err := <-errors; err != nil {
|
||||
return err // NOTE: incorrect: goroutine leak!
|
||||
}
|
||||
}
|
||||
for range filenames {
|
||||
if err := <-errors; err != nil {
|
||||
return err // NOTE: incorrect: goroutine leak!
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
@@ -108,31 +108,30 @@ func makeThumbnails4(filenames []string) error {
|
||||
// makeThumbnails5 makes thumbnails for the specified files in parallel.
|
||||
// It returns the generated file names in an arbitrary order,
|
||||
// or an error if any step failed.
|
||||
|
||||
func makeThumbnails5(filenames []string) (thumbfiles []string, err error) {
|
||||
type item struct {
|
||||
thumbfile string
|
||||
err error
|
||||
}
|
||||
type item struct {
|
||||
thumbfile string
|
||||
err error
|
||||
}
|
||||
|
||||
ch := make(chan item, len(filenames))
|
||||
for _, f := range filenames {
|
||||
go func(f string) {
|
||||
var it item
|
||||
it.thumbfile, it.err = thumbnail.ImageFile(f)
|
||||
ch <- it
|
||||
}(f)
|
||||
}
|
||||
ch := make(chan item, len(filenames))
|
||||
for _, f := range filenames {
|
||||
go func(f string) {
|
||||
var it item
|
||||
it.thumbfile, it.err = thumbnail.ImageFile(f)
|
||||
ch <- it
|
||||
}(f)
|
||||
}
|
||||
|
||||
for range filenames {
|
||||
it := <-ch
|
||||
if it.err != nil {
|
||||
return nil, it.err
|
||||
}
|
||||
thumbfiles = append(thumbfiles, it.thumbfile)
|
||||
}
|
||||
for range filenames {
|
||||
it := <-ch
|
||||
if it.err != nil {
|
||||
return nil, it.err
|
||||
}
|
||||
thumbfiles = append(thumbfiles, it.thumbfile)
|
||||
}
|
||||
|
||||
return thumbfiles, nil
|
||||
return thumbfiles, nil
|
||||
}
|
||||
```
|
||||
|
||||
@@ -144,35 +143,34 @@ func makeThumbnails5(filenames []string) (thumbfiles []string, err error) {
|
||||
// makeThumbnails6 makes thumbnails for each file received from the channel.
|
||||
// It returns the number of bytes occupied by the files it creates.
|
||||
func makeThumbnails6(filenames <-chan string) int64 {
|
||||
sizes := make(chan int64)
|
||||
var wg sync.WaitGroup // number of working goroutines
|
||||
for f := range filenames {
|
||||
wg.Add(1)
|
||||
// worker
|
||||
go func(f string) {
|
||||
defer wg.Done()
|
||||
thumb, err := thumbnail.ImageFile(f)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
info, _ := os.Stat(thumb) // OK to ignore error
|
||||
sizes <- info.Size()
|
||||
}(f)
|
||||
}
|
||||
sizes := make(chan int64)
|
||||
var wg sync.WaitGroup // number of working goroutines
|
||||
for f := range filenames {
|
||||
wg.Add(1)
|
||||
// worker
|
||||
go func(f string) {
|
||||
defer wg.Done()
|
||||
thumb, err := thumbnail.ImageFile(f)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
info, _ := os.Stat(thumb) // OK to ignore error
|
||||
sizes <- info.Size()
|
||||
}(f)
|
||||
}
|
||||
|
||||
// closer
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(sizes)
|
||||
}()
|
||||
// closer
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(sizes)
|
||||
}()
|
||||
|
||||
|
||||
var total int64
|
||||
for size := range sizes {
|
||||
total += size
|
||||
}
|
||||
return total
|
||||
var total int64
|
||||
for size := range sizes {
|
||||
total += size
|
||||
}
|
||||
return total
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user