2020年5月14日木曜日

開発環境

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

コード

package main

import (
 "fmt"
 "strings"
)

func source(s []string, downstream chan<- string) {
 for _, sentence := range s {
  downstream <- sentence
 }
 close(downstream)
}
func splitWords(upstream, downstream chan string) {
 for sentence := range upstream {
  for _, word := range strings.Fields(sentence) {
   downstream <- strings.Trim(word, ",.!?")
  }
 }
 close(downstream)
}
func p(upstream chan string) {
 for word := range upstream {
  fmt.Println(word)
 }
}
func main() {
 s := []string{
  "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.",
 }
 c0 := make(chan string)
 c1 := make(chan string)
 go source(s, c0)
 go splitWords(c0, c1)
 p(c1)
}

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

 % go build splitwords.go
% ./splitwords 
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
% ./splitwords
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 コメント:

コメントを投稿