Skip to content

Commit 1b1d08b

Browse files
committed
Update
1 parent 7638eb3 commit 1b1d08b

File tree

8 files changed

+136
-1
lines changed

8 files changed

+136
-1
lines changed

challenge.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* USAGE: go run challenge.go 1st 2nd 3rd 4th
2+
// Golang program is coded with features commonly needed:
3+
// * command-line arguments (-help, -verbose)
4+
// * Logging
5+
*/
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
"os"
12+
)
13+
14+
func main() {
15+
16+
// The first argument
17+
// is always program name
18+
myProgramName := os.Args[0]
19+
20+
// this will take 4
21+
// command line arguments
22+
cmdArgs := os.Args[4]
23+
24+
// getting the arguments
25+
// with normal indexing
26+
gettingArgs := os.Args[2]
27+
28+
toGetAllArgs := os.Args[1:]
29+
30+
// it will display
31+
// the program name
32+
fmt.Println(myProgramName)
33+
34+
fmt.Println(cmdArgs)
35+
36+
fmt.Println(gettingArgs)
37+
38+
fmt.Println(toGetAllArgs)
39+
}

go-here.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
# go-here.sh in https://github.com/wilsonmar/
4+
# Set variable $GOHOME to this folder as the default folder where custom Go app source code is kept.
5+
# (by default ~/go)
6+
7+
export GOPATH="$HOME/gmail_acct/golang-samples"
8+
go env GOPATH

hello-world.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This is the minimal Go program, to show Go compiler is installed correctly.
2+
package main
3+
import( "fmt"; "time")
4+
var now = time.Now()
5+
func main() {
6+
fmt.Printf("hello, world at %v",now)
7+
}

rand

2.25 MB
Binary file not shown.

range.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
// range to find match of TZ value
23
// From https://www.youtube.com/watch?v=rKnDgT73v8s&t=29m
34
// One of the first Go programs shown.

sample.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// sample.go
2+
// USAGE: go run sample.go add -a=42 -b=23
3+
// go run sample.go mul -a=25 -b=4
4+
5+
package main
6+
7+
import( "fmt"; "time"; "os"; "errors"; "flag"; "math"; "strings")
8+
9+
var now = time.Now() // wall clock
10+
11+
func circleArea(radius float32) (float32, error) {
12+
if radius < 0 {
13+
return 0, errors.New("ERROR: In call of circleArea: radius should be a positive value.")
14+
} else {
15+
return math.Pi * radius * radius, nil
16+
}
17+
}
18+
19+
func main() {
20+
start_time := time.Now() // monotonic clock
21+
args := os.Args[0]
22+
f := strings.Fields(args)
23+
for _, v := range f {
24+
fmt.Println(v) // TODO: output in one
25+
}
26+
fmt.Printf("starting at %v \n", now) // https://golang.org/pkg/time/#Time.Format
27+
28+
argLength := len(os.Args[1:])
29+
if argLength > 0 {
30+
for i, a := range os.Args[1:] {
31+
// TODO: Use defer to concatenate string parts?
32+
fmt.Printf("Arg %d is %s\n", i+1, a)
33+
}
34+
}
35+
36+
// Adapted from https://golangdocs.com/flag-package-golang
37+
// TODO: Add -verbosity
38+
addcmd := flag.NewFlagSet("add", flag.ExitOnError)
39+
a_add := addcmd.Int("a", 0, "The value of a")
40+
b_add := addcmd.Int("b", 0, "The value of b")
41+
42+
mulcmd := flag.NewFlagSet("mul", flag.ExitOnError)
43+
a_mul := mulcmd.Int("a", 0, "The value of a")
44+
b_mul := mulcmd.Int("b", 0, "The value of b")
45+
46+
switch os.Args[1] {
47+
case "add":
48+
addcmd.Parse(os.Args[2:])
49+
fmt.Println(*a_add + *b_add)
50+
case "mul":
51+
mulcmd.Parse(os.Args[2:])
52+
fmt.Println(*(a_mul) * (*b_mul))
53+
default:
54+
fmt.Println("expected add or mul command")
55+
os.Exit(1)
56+
}
57+
58+
// Known error (-3 radius input):
59+
if area2, err := circleArea(-3); err != nil {
60+
fmt.Println(err)
61+
} else {
62+
fmt.Println(area2)
63+
}
64+
65+
t := time.Now()
66+
elapsed := t.Sub(start_time)
67+
fmt.Printf("Elapsed: %v\n", elapsed)
68+
fmt.Printf("Seconds: %f\n", elapsed.Seconds())
69+
fmt.Printf("Minutes: %f\n", elapsed.Minutes())
70+
71+
defer func(msg string) {
72+
if r := recover(); r != nil {
73+
fmt.Println("recovered")
74+
}
75+
fmt.Println(msg)
76+
}("Done.")
77+
78+
// panic("Die!") // to test defer function to recover
79+
}

sample_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sample_test.go

time.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import ( // PROTIP: avoid errors with semicolons vs. commas by using a list:
2222

2323
const (
2424
// For use with http://golang.org/pkg/time/#Parse
25-
timeLayout = "2006-01-02 15:04 MST"
25+
timeLayout = "2020-01-02 15:04 MST"
2626
)
2727

2828
// The one of a few reasons for a global variable:

0 commit comments

Comments
 (0)