計算機科学のブログ

appending issue - Slices - make, literal

Head First GoJay McGavren(著)、O’Reilly Media)の Chapter 6(appending issue - Slices)、p.178(Pool Puzzle)の解答を求めてみる。

コード

main.go

package main

import "fmt"

func main() {
	numbers := make([]float64, 3)
	numbers[0] = 19.7
	numbers[2] = 25.2
	for i, number := range numbers {
		fmt.Println(i, number)
	}
	var letters = []string{"a", "b", "c"}
	for i, letter := range letters {
		fmt.Println(i, letter)
	}
}

入出力結果(Terminal, Zsh)

% go run ./main.go 
0 19.7
1 0
2 25.2
0 a
1 b
2 c
%