開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES) (Alan A.A. Donovan(著)、Brian W. Kernighan(著)、柴田 芳樹(翻訳)、丸善出版)の第4章(コンポジット型)、4.3(マップ)、練習問題4.9の解答を求めてみる。
コード
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
// 日本語のコメント
counts := map[string]int{}
invalid := 0
input := bufio.NewScanner(os.Stdin)
input.Split(bufio.ScanWords)
for input.Scan() {
counts[input.Text()]++
}
for c, n := range counts {
fmt.Printf("%-30s\t%5d\n", c, n)
}
if invalid > 0 {
fmt.Printf("\n%d invalid UTF-8 characters\n", invalid)
}
}
入出力結果(cmd(コマンドプロンプト)、Terminal)
C:\Users\...>go run wordfreq.go < wordfreq.go
"fmt" 1
main() 1
if 1
UTF-8 1
( 1
) 1
// 1
counts 2
invalid 3
0 2
input 1
"bufio" 1
{ 4
bufio.NewScanner(os.Stdin) 1
input.Scan() 1
counts[input.Text()]++ 1
package 1
import 1
func 1
c, 2
:= 4
invalid) 1
"os" 1
日本語のコメント 1
n) 1
> 1
} 4
n 1
input.Split(bufio.ScanWords) 1
for 2
range 1
fmt.Printf("%-30s\t%5d\n", 1
fmt.Printf("\n%d 1
characters\n", 1
main 1
map[string]int{} 1
C:\Users\...>
0 コメント:
コメントを投稿