2020年6月17日水曜日

開発環境

Go Systems Programming: Master Linux and Unix system level programming with Go (Mihalis Tsoukalos(著)、Packt Publishing)のChapter 5(Files and Directories)、Exercises 3.の解答を求めてみる。

コード

package main

import (
 "flag"
 "fmt"
 "os"
)

func main() {
 minusV := flag.Bool(
  "v",
  false,
  "Be verbose when deleting files, showing them as they are removed.",
 )
 flag.Parse()
 flags := flag.Args()
 if len(flags) < 1 {
  fmt.Fprintln(os.Stderr,
   "usage: rm [-v] file ...\n       unlink file")
  os.Exit(1)
 }
 for _, file := range flags {
  err := os.Remove(file)
  if err != nil {
   fmt.Fprintln(os.Stderr, err)
  } else if *minusV {
   fmt.Println(file)
  }
 }
}

入出力結果(Zsh、PowerShell、Terminal)

% go build rm.go
% touch temp{1,2,3,4,5,6,7,8,9,0}.txt && ls
./  rm.go  temp2.txt temp5.txt temp8.txt
../  temp0.txt temp3.txt temp6.txt temp9.txt
rm*  temp1.txt temp4.txt temp7.txt
% ./rm temp0.txt      
% ls
./  rm.go  temp3.txt temp6.txt temp9.txt
../  temp1.txt temp4.txt temp7.txt
rm*  temp2.txt temp5.txt temp8.txt
% ./rm -v temp1.txt 
temp1.txt
% ./rm temp2.txt temp3.txt 
% ls
./  rm*  temp4.txt temp6.txt temp8.txt
../  rm.go  temp5.txt temp7.txt temp9.txt
% ./rm -v temp4.txt temp5.txt 
temp4.txt
temp5.txt
% ls
./  rm*  temp6.txt temp8.txt
../  rm.go  temp7.txt temp9.txt
% ./rm a
remove a: no such file or directory
% ls
./  rm*  temp6.txt temp8.txt
../  rm.go  temp7.txt temp9.txt
% ./rm a temp6.txt 
remove a: no such file or directory
% ls
./  rm*  temp7.txt temp9.txt
../  rm.go  temp8.txt
% ./rm -v a temp7.txt 
remove a: no such file or directory
temp7.txt
% ls
./  rm*  temp8.txt
../  rm.go  temp9.txt
% ./rm -v temp*.txt
temp8.txt
temp9.txt
% ls
./ ../ rm* rm.go
% ./rm -v a b c d e
remove a: no such file or directory
remove b: no such file or directory
remove c: no such file or directory
remove d: no such file or directory
remove e: no such file or directory
% ./rm 
usage: rm [-v] file ...
       unlink file
% ./rm -v
usage: rm [-v] file ...
       unlink file
%

0 コメント:

コメントを投稿