開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
Go Systems Programming: Master Linux and Unix system level programming with Go (Mihalis Tsoukalos(著)、Packt Publishing)のChapter 2(Writing Programs in Go)、Exercises 6.の解答を求めてみる。
コード
package main
import (
"fmt"
"strconv"
)
func arrayToMap(a [4]int) map[string]int {
m := make(map[string]int)
for i, v := range a {
m[strconv.Itoa(i)] = v
}
return m
}
func p(m map[string]int) {
for k, v := range m {
fmt.Printf("%v: %v\n", k, v)
}
}
func main() {
ary := [4]int{1, -2, 14, 0}
m := arrayToMap(ary)
p(m)
}
入出力結果(Zsh、PowerShell、Terminal)
% go build
% ./array2map
2: 14
3: 0
0: 1
1: -2
% ./array2map
3: 0
0: 1
1: -2
2: 14
% ./array2map
0 コメント:
コメントを投稿