Posted on

Tutorial for Go lang

備忘録。以下、Echo より

package main

import (
   "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.

func Parse()
Parse parses the command-line flags. Must be called after all flags are defined and before any are accessed by the program.

func NArg() int
NArg is the number of arguments remaining after flags have been processed.

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)は最初の残っている引数。

シンプルだなー go lang