From d07905842e192e4867819a7bb3ce98493139d402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Fri, 4 Nov 2022 18:28:16 +0900 Subject: [PATCH 1/5] initial commit --- go/study/ch03_file_io/go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 go/study/ch03_file_io/go.mod diff --git a/go/study/ch03_file_io/go.mod b/go/study/ch03_file_io/go.mod new file mode 100644 index 00000000..816ff56c --- /dev/null +++ b/go/study/ch03_file_io/go.mod @@ -0,0 +1,3 @@ +module github.com/tecyokomichi/WebAppLearning/go/study/file_io + +go 1.19 From c73af6bb0fcdfb8a51942761ba4997b08c0de664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Wed, 16 Nov 2022 08:46:04 +0900 Subject: [PATCH 2/5] =?UTF-8?q?:+1:=20=E3=82=B5=E3=83=96=E3=82=B3=E3=83=9E?= =?UTF-8?q?=E3=83=B3=E3=83=89=20cat=20=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch03_file_io/main.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 go/study/ch03_file_io/main.go diff --git a/go/study/ch03_file_io/main.go b/go/study/ch03_file_io/main.go new file mode 100644 index 00000000..244f0703 --- /dev/null +++ b/go/study/ch03_file_io/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "os" +) + +func main() { + + switch os.Args[1] { + case "cat": + if len(os.Args) != 3 { + showHelp() + os.Exit(1) + } + f, err := os.ReadFile(os.Args[2]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + fmt.Println(string(f)) + default: + showHelp() + os.Exit(1) + } +} + +func showHelp() { + fmt.Printf("USAGE:\n") + fmt.Printf("%s cat FILE\n", os.Args[0]) +} From 9aeb113865abf38cd979c2d9c338785c7de39c50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Wed, 16 Nov 2022 08:53:30 +0900 Subject: [PATCH 3/5] =?UTF-8?q?:+1:=20=E3=82=B5=E3=83=96=E3=82=B3=E3=83=9E?= =?UTF-8?q?=E3=83=B3=E3=83=89=20write=20=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch03_file_io/main.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/go/study/ch03_file_io/main.go b/go/study/ch03_file_io/main.go index 244f0703..663950d9 100644 --- a/go/study/ch03_file_io/main.go +++ b/go/study/ch03_file_io/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "strings" ) func main() { @@ -19,6 +20,33 @@ func main() { os.Exit(1) } fmt.Println(string(f)) + case "write": + if len (os.Args) != 4 { + showHelp() + os.Exit(1) + } + _, e := os.Stat(os.Args[2]) + if e == nil { + fmt.Printf("%s already exists. Overwrite? (y/n): ", os.Args[2]) + var res string + fmt.Scanln(&res) + if !strings.HasPrefix(strings.ToLower(res), "y") { + fmt.Println("Quit writing") + os.Exit(1) + } + } + f, err := os.Create(os.Args[2]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + defer f.Close() + _, ee := fmt.Fprintln(f, os.Args[3]) + if ee != nil{ + fmt.Fprintln(os.Stderr, ee) + os.Exit(1) + } + default: showHelp() os.Exit(1) From ab5ff0e6780a4f07f288b2e3d36b3b8f1b6e4f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Wed, 16 Nov 2022 12:39:16 +0900 Subject: [PATCH 4/5] =?UTF-8?q?:+1:=20=E3=82=B5=E3=83=96=E3=82=B3=E3=83=9E?= =?UTF-8?q?=E3=83=B3=E3=83=89=20write=20=E3=83=95=E3=82=A1=E3=82=A4?= =?UTF-8?q?=E3=83=AB=E3=81=AE=E5=AD=98=E5=9C=A8=E3=83=81=E3=82=A7=E3=83=83?= =?UTF-8?q?=E3=82=AF=E8=A6=8B=E7=9B=B4=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch03_file_io/main.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/go/study/ch03_file_io/main.go b/go/study/ch03_file_io/main.go index 663950d9..a9b4b681 100644 --- a/go/study/ch03_file_io/main.go +++ b/go/study/ch03_file_io/main.go @@ -25,8 +25,12 @@ func main() { showHelp() os.Exit(1) } - _, e := os.Stat(os.Args[2]) - if e == nil { + if _, err := os.Stat(os.Args[2]); err != nil { + if !os.IsNotExist(err) { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + } else { fmt.Printf("%s already exists. Overwrite? (y/n): ", os.Args[2]) var res string fmt.Scanln(&res) From b14ce71a7f1a030e4f3bcecaa6f58dbf3fa290ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Wed, 16 Nov 2022 12:42:06 +0900 Subject: [PATCH 5/5] =?UTF-8?q?:+1:=20=E3=82=B5=E3=83=96=E3=82=B3=E3=83=9E?= =?UTF-8?q?=E3=83=B3=E3=83=89=20append=20=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch03_file_io/main.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/go/study/ch03_file_io/main.go b/go/study/ch03_file_io/main.go index a9b4b681..9369a699 100644 --- a/go/study/ch03_file_io/main.go +++ b/go/study/ch03_file_io/main.go @@ -50,7 +50,22 @@ func main() { fmt.Fprintln(os.Stderr, ee) os.Exit(1) } - + case "append": + if len (os.Args) != 4 { + showHelp() + os.Exit(1) + } + f, err := os.OpenFile(os.Args[2], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0664) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + defer f.Close() + _, e := fmt.Fprintln(f, os.Args[3]) + if e != nil{ + fmt.Fprintln(os.Stderr, e) + os.Exit(1) + } default: showHelp() os.Exit(1)