Skip to content

Commit 1bcfdd0

Browse files
rscgopherbot
authored andcommitted
hello: create new standalone module example
We want to revive golang.org/x/example as a test case for an experiment with a 'gonew' command. This CL changes the hello world program to be standalone and demonstrate a bit more about flag parsing and command-line logging. Change-Id: Iee481344c801f046813806a537d59e3242f9152a Reviewed-on: https://go-review.googlesource.com/c/example/+/513996 Reviewed-by: Cameron Balahan <cbalahan@google.com> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Russ Cox <rsc@golang.org>
1 parent 00c7068 commit 1bcfdd0

File tree

5 files changed

+80
-15
lines changed

5 files changed

+80
-15
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@ $ cd example
1313
```
1414
https://go.googlesource.com/example is the canonical Git repository.
1515
It is mirrored at https://github.com/golang/example.
16-
## [hello](hello/) and [stringutil](stringutil/)
16+
17+
## [hello](hello/) and [hello/reverse](hello/reverse/)
1718

1819
```
1920
$ cd hello
2021
$ go build
22+
$ ./hello -help
2123
```
22-
A trivial "Hello, world" program that uses a stringutil package.
24+
A trivial "Hello, world" program that uses a library package.
2325

24-
Command [hello](hello/) covers:
26+
The [hello](hello/) command covers:
2527

2628
* The basic form of an executable command
2729
* Importing packages (from the standard library and the local repository)
2830
* Printing strings ([fmt](//golang.org/pkg/fmt/))
31+
* Command-line flags ([flag](//golang.org/pkg/flag/))
32+
* Logging ([log](//golang.org/pkg/log/))
2933

30-
Library [stringutil](stringutil/) covers:
34+
The [reverse](hello/reverse/) reverse covers:
3135

3236
* The basic form of a library
3337
* Conversion between string and []rune
@@ -37,7 +41,7 @@ Library [stringutil](stringutil/) covers:
3741

3842
```
3943
$ cd outyet
40-
$ go build
44+
$ go run .
4145
```
4246
A web server that answers the question: "Is Go 1.x out yet?"
4347

hello/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module golang.org/x/example/hello
2+
3+
go 1.19
4+

hello/hello.go

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,71 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// Hello is a hello, world program, demonstrating
6+
// how to write a simple command-line program.
7+
//
8+
// Usage:
9+
//
10+
// hello [options] [name]
11+
//
12+
// The options are:
13+
//
14+
// -g greeting
15+
// Greet with the given greeting, instead of "Hello".
16+
//
17+
// -r
18+
// Greet in reverse.
19+
//
20+
// By default, hello greets the world.
21+
// If a name is specified, hello greets that name instead.
522
package main
623

724
import (
25+
"flag"
826
"fmt"
27+
"log"
28+
"os"
929

10-
"golang.org/x/example/stringutil"
30+
"golang.org/x/example/hello/reverse"
31+
)
32+
33+
func usage() {
34+
fmt.Fprintf(os.Stderr, "usage: hello [options] [name]\n")
35+
flag.PrintDefaults()
36+
os.Exit(2)
37+
}
38+
39+
var (
40+
greeting = flag.String("g", "Hello", "Greet with `greeting`")
41+
reverseFlag = flag.Bool("r", false, "Greet in reverse")
1142
)
1243

1344
func main() {
14-
fmt.Println(stringutil.Reverse("!selpmaxe oG ,olleH"))
45+
// Configure logging for a command-line program.
46+
log.SetFlags(0)
47+
log.SetPrefix("hello: ")
48+
49+
// Parse flags.
50+
flag.Usage = usage
51+
flag.Parse()
52+
53+
// Parse and validate arguments.
54+
name := "world"
55+
args := flag.Args()
56+
if len(args) >= 2 {
57+
usage()
58+
}
59+
if len(args) >= 1 {
60+
name = args[0]
61+
}
62+
if name == "" { // hello '' is an error
63+
log.Fatalf("invalid name %q", name)
64+
}
65+
66+
// Run actual logic.
67+
if *reverseFlag {
68+
fmt.Printf("%s, %s!\n", reverse.String(*greeting), reverse.String(name))
69+
return
70+
}
71+
fmt.Printf("%s, %s!\n", *greeting, name)
1572
}

stringutil/reverse.go renamed to hello/reverse/reverse.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// Package stringutil contains utility functions for working with strings.
6-
package stringutil
5+
// Package reverse can reverse things, particularly strings.
6+
package reverse
77

8-
// Reverse returns its argument string reversed rune-wise left to right.
9-
func Reverse(s string) string {
8+
// String returns its argument string reversed rune-wise left to right.
9+
func String(s string) string {
1010
r := []rune(s)
1111
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
1212
r[i], r[j] = r[j], r[i]

stringutil/reverse_test.go renamed to hello/reverse/reverse_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package stringutil
5+
package reverse
66

77
import "testing"
88

9-
func TestReverse(t *testing.T) {
9+
func TestString(t *testing.T) {
1010
for _, c := range []struct {
1111
in, want string
1212
}{
1313
{"Hello, world", "dlrow ,olleH"},
1414
{"Hello, 世界", "界世 ,olleH"},
1515
{"", ""},
1616
} {
17-
got := Reverse(c.in)
17+
got := String(c.in)
1818
if got != c.want {
19-
t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)
19+
t.Errorf("String(%q) == %q, want %q", c.in, got, c.want)
2020
}
2121
}
2222
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy