計算機科学のブログ

let's get going - Syntax Basics - Declaring variables, var keyword

Head First GoJay McGavren(著)、O’Reilly Media)の Chapter 1(let’s get going - Syntax Basics)、p.18(Code Magnets)の解答を求めてみる。

コード

sample.go

package main

import "fmt"

func main() {
	var originalCount int = 10
	var eatenCount int = 4
	fmt.Println("I started with", originalCount, "apples.")
	fmt.Println("Some jerk ate", eatenCount, "apples.")
	fmt.Println("There are", originalCount-eatenCount, "apples left.")
}

入出力結果(Terminal, Zsh)

% go run ./sample.go
I started with 10 apples.
Some jerk ate 4 apples.
There are 6 apples left.
%