備忘録。以下、Echo より
—–
package main
import (
"os";
"flag"; // command line option parser
)
var omitNewline = flag.Bool("n", false, "don't print final newline")
const (
Space = " ";
Newline = "\n";
)
func main() {
flag.Parse(); // Scans the arg list and set up flags
var s string = "";
for i := 0; i < flag.NArg(); i++ {
if i > 0 {
s += Space
}
s += flag.Arg(i)
}
if !*omitNewline {
s += Newline // 改行
}
os.Stdout.WriteString(s); // 文字列出力
}
—–
コマンドラインで渡されたパラメータを出力するだけのプラグラム。
主に使われているパッケージは以下の通り。日本語はかなり適当。
func Bool(name string, value bool, usage string) *bool
Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag.
Boolは指定された名前は、デフォルト値、および使用状況の文字列でboolのフラグを定義します。戻り値はbool変数のアドレスは、フラグの値を格納します。
func Parse()
Parse parses the command-line flags. Must be called after all flags are defined and before any are accessed by the program.
Parseはコマンドラインフラグを解析します。後にすべてのフラグと定義される前に、任意のプログラムによってアクセスされる必要があります。
func NArg() int
NArg is the number of arguments remaining after flags have been processed.
NArgはフラグが処理された後に、残っている引数の数です。
func Arg(i int) string
Arg returns the i’th command-line argument. Arg(0) is the first remaining argument after flags have been processed.
Argはi番目のコマンドライン引数を返します。 フラグが処理された後、Arg(0)は最初の残っている引数。
あとfor文に括弧を付けない(CでもPHPでも付けるのに)ことに違和感を感じてしまった。
シンプルだなー go lang