Go - (Package) main

About

Language - (Main|Application Entry point) in Go.

Package main is special. It defines a standalone executable program, not a library.

Argument

Os.Args variable

  • Reprint the argument. Os.Args is an array
var s, sep string
for i := 1; i < len(os.Args); i++ {
	s += sep + os.Args[i]
	sep = " "
}
fmt.Println(s)

Argument Parsing with the flag package

package main

import (
	"flag"
	"fmt"
	"strings"
)

var n = flag.Bool("n", false, "omit trailing newline")
var sep = flag.String("s", " ", "separator")

func main() {
	flag.Parse()
	fmt.Print(strings.Join(flag.Args(), *sep))
	if !*n {
		fmt.Println()
	}
}

Documentation / Reference


Powered by ComboStrap