計算機科学のブログ

which code runs next - Conditionals and Loops - nil, logging a fatal error

Head First GoJay McGavren(著)、O’Reilly Media)の Chapter 2(which code runs next? Conditionals and Loops)、p.41(Exercise)の解答を求めてみる。

コード

sample.go

package main

import (
	"fmt"
	"log"
	"os"
)

func main() {
	fileInfo, err := os.Stat("my.txt")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(fileInfo.Size())
}

入出力結果(Terminal, Zsh)

% go run ./sample.go 
2024/12/01 12:32:13 stat my.txt: no such file or directory
exit status 1
% touch my.txt      
% go run ./sample.go
0
% echo 'hello' > my.txt 
% go run ./sample.go   
6
%