2020年2月15日土曜日

開発環境

入門Goプログラミング (Nathan Youngman(著)、Roger Peppé(著)、吉川 邦夫(監修, 翻訳)、翔泳社)のUNIT 7(並行プログラミング)、LESSON 30(ゴルーチンと並行性)の練習問題-2の解答を求めてみる。

コード

package main

import (
 "fmt"
 "strings"
)

func splitWords(s string, c chan string) {
 for _, word := range strings.Fields(s) {
  c <- strings.Trim(word, ",.?!")
 }
 close(c)
}
func printWords(c chan string) {
 for v := range c {
  fmt.Println(v)
 }
}
func main() {
 s := `As far as eye could reach he saw nothing but the stems of the great plants about him receding in the violet shade, and far overhead the multiple transparency of huge leaves filtering the sunshine to the solemn splendour of twilight in which he walked. Whenever he felt able he ran again; the ground continued soft and springy, covered with the same resilient weed which was the first thing his hands had touched in Malacandra. Once or twice a small red creature scuttled across his path, but otherwise there seemed to be no life stirring in the wood; nothing to fear—except the fact of wandering unprovisioned and alone in a forest of unknown vegetation thousands or millions of miles beyond the reach or knowledge of man.`
 c := make(chan string)
 go splitWords(s, c)
 printWords(c)
}

入出力結果(Zsh、PowerShell、Terminal)

% go run split-words.go
As
far
as
eye
could
reach
he
saw
nothing
but
the
stems
of
the
great
plants
about
him
receding
in
the
violet
shade
and
far
overhead
the
multiple
transparency
of
huge
leaves
filtering
the
sunshine
to
the
solemn
splendour
of
twilight
in
which
he
walked
Whenever
he
felt
able
he
ran
again;
the
ground
continued
soft
and
springy
covered
with
the
same
resilient
weed
which
was
the
first
thing
his
hands
had
touched
in
Malacandra
Once
or
twice
a
small
red
creature
scuttled
across
his
path
but
otherwise
there
seemed
to
be
no
life
stirring
in
the
wood;
nothing
to
fear—except
the
fact
of
wandering
unprovisioned
and
alone
in
a
forest
of
unknown
vegetation
thousands
or
millions
of
miles
beyond
the
reach
or
knowledge
of
man
%

0 コメント:

コメントを投稿