Go
Beginner
1 min read
The Go Toolchain
Example
// Demonstrate common go tool commands via code comments and a real program.
// $ go build -o myapp . — compile to binary named "myapp"
// $ go run main.go — compile and run in one step
// $ go test ./... — run all tests recursively
// $ go fmt ./... — format all Go files
// $ go vet ./... — static analysis
// $ go mod tidy — add/remove module dependencies
// $ go doc fmt.Println — show docs for fmt.Println
package main
import (
"flag"
"fmt"
"log"
"os"
)
func main() {
// flag demonstrates idiomatic Go CLI argument parsing
name := flag.String("name", "world", "whom to greet")
verbose := flag.Bool("v", false, "enable verbose output")
flag.Parse()
if *verbose {
log.SetOutput(os.Stderr)
log.Printf("Running with name=%q\n", *name)
}
fmt.Printf("Hello, %s!\n", *name)
}
// Build and run:
// $ go build -o greet .
// $ ./greet -name=Go -v