diff --git a/hello/go.mod b/hello/go.mod deleted file mode 100644 index eb0dea6..0000000 --- a/hello/go.mod +++ /dev/null @@ -1,4 +0,0 @@ -module golang.org/x/example/hello - -go 1.19 - diff --git a/hello/hello.go b/hello/hello.go deleted file mode 100644 index ce2a423..0000000 --- a/hello/hello.go +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2023 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Hello is a hello, world program, demonstrating -// how to write a simple command-line program. -// -// Usage: -// -// hello [options] [name] -// -// The options are: -// -// -g greeting -// Greet with the given greeting, instead of "Hello". -// -// -r -// Greet in reverse. -// -// By default, hello greets the world. -// If a name is specified, hello greets that name instead. -package main - -import ( - "flag" - "fmt" - "log" - "os" - - "golang.org/x/example/hello/reverse" -) - -func usage() { - fmt.Fprintf(os.Stderr, "usage: hello [options] [name]\n") - flag.PrintDefaults() - os.Exit(2) -} - -var ( - greeting = flag.String("g", "Hello", "Greet with `greeting`") - reverseFlag = flag.Bool("r", false, "Greet in reverse") -) - -func main() { - // Configure logging for a command-line program. - log.SetFlags(0) - log.SetPrefix("hello: ") - - // Parse flags. - flag.Usage = usage - flag.Parse() - - // Parse and validate arguments. - name := "world" - args := flag.Args() - if len(args) >= 2 { - usage() - } - if len(args) >= 1 { - name = args[0] - } - if name == "" { // hello '' is an error - log.Fatalf("invalid name %q", name) - } - - // Run actual logic. - if *reverseFlag { - fmt.Printf("%s, %s!\n", reverse.String(*greeting), reverse.String(name)) - return - } - fmt.Printf("%s, %s!\n", *greeting, name) -} diff --git a/hello/reverse/example_test.go b/hello/reverse/example_test.go deleted file mode 100644 index a0df581..0000000 --- a/hello/reverse/example_test.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2023 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package reverse_test - -import ( - "fmt" - - "golang.org/x/example/hello/reverse" -) - -func ExampleString() { - fmt.Println(reverse.String("hello")) - // Output: olleh -} diff --git a/hello/reverse/reverse.go b/hello/reverse/reverse.go deleted file mode 100644 index b6fe74a..0000000 --- a/hello/reverse/reverse.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2023 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package reverse can reverse things, particularly strings. -package reverse - -// String returns its argument string reversed rune-wise left to right. -func String(s string) string { - r := []rune(s) - for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { - r[i], r[j] = r[j], r[i] - } - return string(r) -} diff --git a/hello/reverse/reverse_test.go b/hello/reverse/reverse_test.go deleted file mode 100644 index d130117..0000000 --- a/hello/reverse/reverse_test.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2023 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package reverse - -import "testing" - -func TestString(t *testing.T) { - for _, c := range []struct { - in, want string - }{ - {"Hello, world", "dlrow ,olleH"}, - {"Hello, 世界", "界世 ,olleH"}, - {"", ""}, - } { - got := String(c.in) - if got != c.want { - t.Errorf("String(%q) == %q, want %q", c.in, got, c.want) - } - } -}