計算機科学のブログ

let's get going - Syntax Basics - Conversons, math operations

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

コード

sample.go

package main

import "fmt"

func main() {
	price := 100
	fmt.Println("Price is", price, "dollars.")
	taxRate := 0.08
	tax := float64(price) * taxRate
	fmt.Println("Tax is", tax, "dollars.")
	total := float64(price) + tax
	fmt.Println("Total cost is", total, "dollars.")
	availableFunds := 120
	fmt.Println(availableFunds, "dollars available.")
	fmt.Println("Within budget?", total <= float64(availableFunds))
}

入出力結果(Terminal, Zsh)

% go run ./sample.go 
Price is 100 dollars.
Tax is 8 dollars.
Total cost is 108 dollars.
120 dollars available.
Within budget? true
%