開発環境
- macOS Catalina - Apple (OS)
 - Emacs (Text Editor)
 - Windows 10 Pro (OS)
 - Visual Studio Code (Text Editor)
 - Go (プログラミング言語)
 
Go Systems Programming: Master Linux and Unix system level programming with Go (Mihalis Tsoukalos(著)、Packt Publishing)のChapter 1(Getting Started with Go and Unix Systems Programming)、Exercisesの解答を求めてみる。
コード
package main
import (
 "fmt"
 "io/ioutil"
 "os"
)
func main() {
 fmt.Println("3.")
 fmt.Println("Hello, World!")
 fmt.Println("7.")
 if len(os.Args) < 2 {
  fmt.Fprintln(os.Stderr, "Usage: sample1 <filename>")
  os.Exit(1)
 }
 input, err := ioutil.ReadFile(os.Args[1])
 if err != nil {
  fmt.Fprintln(os.Stderr, err)
  os.Exit(1)
 }
 fmt.Println(string(input))
}
入出力結果(Zsh、PowerShell、Terminal)
% go build sample1.go
% ./sample1          
3.
Hello, World!
7.
Usage: sample1 <filename>
% ./sample1 a
3.
Hello, World!
7.
open a: no such file or directory
% ./sample1 sample1.go
3.
Hello, World!
7.
package main
import (
 "fmt"
 "io/ioutil"
 "os"
)
func main() {
 fmt.Println("3.")
 fmt.Println("Hello, World!")
 fmt.Println("7.")
 if len(os.Args) < 2 {
  fmt.Fprintln(os.Stderr, "Usage: sample1 <filename>")
  os.Exit(1)
 }
 input, err := ioutil.ReadFile(os.Args[1])
 if err != nil {
  fmt.Fprintln(os.Stderr, err)
  os.Exit(1)
 }
 fmt.Println(string(input))
}
% 
0 コメント:
コメントを投稿