From 441b3a9d5475f5e0f9ce53848ccfcb558f407cb0 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sun, 25 Feb 2024 13:47:12 -0500 Subject: [PATCH] Update project structure --- Makefile.template | 6 ++ .../intro_course1/week3/assignments/slice.go | 44 +++++++----- .../intro_course1/week3/assignments/slice2.go | 66 +++++++++++++++++ coursera/week3/assignments/slice_fail.go | 67 ++++++++++++++++++ {ch1 => idiomatic_go/ch1}/.gitignore | 0 {ch1 => idiomatic_go/ch1}/Makefile | 0 {ch1 => idiomatic_go/ch1}/go.mod | 0 {ch1 => idiomatic_go/ch1}/hello.go | 0 {ch3 => idiomatic_go/ch3}/Makefile | 0 {ch3 => idiomatic_go/ch3}/go.mod | 0 {ch3 => idiomatic_go/ch3}/my_slice | Bin {ch3 => idiomatic_go/ch3}/slice.go | 0 {ch5 => idiomatic_go/ch5}/README.md | 0 {ch5 => idiomatic_go/ch5}/func_thing.go | 0 {ch5 => idiomatic_go/ch5}/func_thing_test.go | 0 {ch5 => idiomatic_go/ch5}/go.mod | 0 learn_go_with_tests/helloworld/go.mod | 3 + learn_go_with_tests/helloworld/hello.go | 12 ++++ learn_go_with_tests/helloworld/hello_test.go | 12 ++++ scratch_space/request.go | 36 ++++++++++ 20 files changed, 227 insertions(+), 19 deletions(-) create mode 100644 coursera/intro_course1/week3/assignments/slice2.go create mode 100644 coursera/week3/assignments/slice_fail.go rename {ch1 => idiomatic_go/ch1}/.gitignore (100%) rename {ch1 => idiomatic_go/ch1}/Makefile (100%) rename {ch1 => idiomatic_go/ch1}/go.mod (100%) rename {ch1 => idiomatic_go/ch1}/hello.go (100%) rename {ch3 => idiomatic_go/ch3}/Makefile (100%) rename {ch3 => idiomatic_go/ch3}/go.mod (100%) rename {ch3 => idiomatic_go/ch3}/my_slice (100%) rename {ch3 => idiomatic_go/ch3}/slice.go (100%) rename {ch5 => idiomatic_go/ch5}/README.md (100%) rename {ch5 => idiomatic_go/ch5}/func_thing.go (100%) rename {ch5 => idiomatic_go/ch5}/func_thing_test.go (100%) rename {ch5 => idiomatic_go/ch5}/go.mod (100%) create mode 100644 learn_go_with_tests/helloworld/go.mod create mode 100644 learn_go_with_tests/helloworld/hello.go create mode 100644 learn_go_with_tests/helloworld/hello_test.go create mode 100644 scratch_space/request.go diff --git a/Makefile.template b/Makefile.template index 5d74391..41a3f0e 100644 --- a/Makefile.template +++ b/Makefile.template @@ -19,3 +19,9 @@ build: vet clean: rm rf ./hello_world .PHONY:clean + + +docs: + echo "Starting godocs on http://localhost:8000" + godoc -http=:8000 +.PHONY \ No newline at end of file diff --git a/coursera/intro_course1/week3/assignments/slice.go b/coursera/intro_course1/week3/assignments/slice.go index e74e1c4..16d0adc 100644 --- a/coursera/intro_course1/week3/assignments/slice.go +++ b/coursera/intro_course1/week3/assignments/slice.go @@ -16,19 +16,6 @@ import ( "strings" ) -func stringsToIntegers(inputStrings []string) []int { - integers := make([]int, len(inputStrings)) - for i, v := range inputStrings { - intValue, err := strconv.Atoi(v) - if err != nil { - fmt.Fprintf(os.Stderr, "Error: %s could not be converted to integer", v) - os.Exit(1) - } - integers[i] = intValue - } - return integers -} - func sortIntegers(integers []int) { // Insertion sort O(n^2) i := 1 @@ -50,11 +37,30 @@ func sortIntegers(integers []int) { func main() { inputReader := bufio.NewReader(os.Stdin) - fmt.Println("Enter a line of text: ") - line, _ := inputReader.ReadString('\n') + var integers []int + + for { + fmt.Println("Enter a integer of text: ") + line, err := inputReader.ReadString('\n') + + if err != nil { + fmt.Fprintf(os.Stderr, "An error occurred with your input.") + } + + if strings.Contains(line, "X") { + fmt.Println("Closing...") + os.Exit(0) + } + + inputInteger, err := strconv.Atoi(strings.TrimSpace(line)) - integersList := stringsToIntegers(strings.Split(line[:len(line)-1], " ")) - fmt.Println("You entered:", integersList) - sortIntegers(integersList) - fmt.Println("Those sorted are:", integersList) + if err != nil { + fmt.Fprintf(os.Stderr, "Could not convert %s to integer", strings.TrimSpace(line)) + continue + } + + integers = append(integers, inputInteger) + sortIntegers(integers) + fmt.Println(integers) + } } diff --git a/coursera/intro_course1/week3/assignments/slice2.go b/coursera/intro_course1/week3/assignments/slice2.go new file mode 100644 index 0000000..16d0adc --- /dev/null +++ b/coursera/intro_course1/week3/assignments/slice2.go @@ -0,0 +1,66 @@ +// A maximum of 3 points will be given for the first test execution, if the program correctly prints the sorted slice +// after entering three distinct integers. **Points are awarded incrementally each time that an integer +// is added and it correctly prints the sorted slice. + +//A maximum of 2 points will be given for the second test execution, +// if the program correctly prints the sorted slice after entering four distinct integers. +// **Points are awarded if it correctly prints the sorted slice after adding the fourth integer. + +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func sortIntegers(integers []int) { + // Insertion sort O(n^2) + i := 1 + for i < len(integers) { + insert := integers[i] + j := i - 1 + for j >= 0 { + if insert < integers[j] { + integers[j+1] = integers[j] + j -= 1 + } else { + break + } + } + integers[j+1] = insert + i += 1 + } +} + +func main() { + inputReader := bufio.NewReader(os.Stdin) + var integers []int + + for { + fmt.Println("Enter a integer of text: ") + line, err := inputReader.ReadString('\n') + + if err != nil { + fmt.Fprintf(os.Stderr, "An error occurred with your input.") + } + + if strings.Contains(line, "X") { + fmt.Println("Closing...") + os.Exit(0) + } + + inputInteger, err := strconv.Atoi(strings.TrimSpace(line)) + + if err != nil { + fmt.Fprintf(os.Stderr, "Could not convert %s to integer", strings.TrimSpace(line)) + continue + } + + integers = append(integers, inputInteger) + sortIntegers(integers) + fmt.Println(integers) + } +} diff --git a/coursera/week3/assignments/slice_fail.go b/coursera/week3/assignments/slice_fail.go new file mode 100644 index 0000000..ef2fc5e --- /dev/null +++ b/coursera/week3/assignments/slice_fail.go @@ -0,0 +1,67 @@ +// A maximum of 3 points will be given for the first test execution, if the program correctly prints the sorted slice +// after entering three distinct integers. **Points are awarded incrementally each time that an integer +// is added and it correctly prints the sorted slice. + +//A maximum of 2 points will be given for the second test execution, +// if the program correctly prints the sorted slice after entering four distinct integers. +// **Points are awarded if it correctly prints the sorted slice after adding the fourth integer. + +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func stringsToIntegers(inputStrings []string) []int { + integers := make([]int, len(inputStrings)) + for i, v := range inputStrings { + intValue, err := strconv.Atoi(v) + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %s could not be converted to integer", v) + os.Exit(1) + } + integers[i] = intValue + } + return integers +} + +func sortIntegers(integers []int) { + // Insertion sort O(n^2) + i := 1 + for i < len(integers) { + insert := integers[i] + j := i - 1 + for j >= 0 { + if insert < integers[j] { + integers[j+1] = integers[j] + j -= 1 + } else { + break + } + } + integers[j+1] = insert + i += 1 + } +} + +func main() { + inputReader := bufio.NewReader(os.Stdin) + var integers []int + + for { + fmt.Println("Enter a integer of text: ") + line, _ := inputReader.ReadString('\n') + + } + fmt.Println("Enter a line of text: ") + line, _ := inputReader.ReadString('\n') + + integersList := stringsToIntegers(strings.Split(line[:len(line)-1], " ")) + fmt.Println("You entered:", integersList) + sortIntegers(integersList) + fmt.Println("Those sorted are:", integersList) +} diff --git a/ch1/.gitignore b/idiomatic_go/ch1/.gitignore similarity index 100% rename from ch1/.gitignore rename to idiomatic_go/ch1/.gitignore diff --git a/ch1/Makefile b/idiomatic_go/ch1/Makefile similarity index 100% rename from ch1/Makefile rename to idiomatic_go/ch1/Makefile diff --git a/ch1/go.mod b/idiomatic_go/ch1/go.mod similarity index 100% rename from ch1/go.mod rename to idiomatic_go/ch1/go.mod diff --git a/ch1/hello.go b/idiomatic_go/ch1/hello.go similarity index 100% rename from ch1/hello.go rename to idiomatic_go/ch1/hello.go diff --git a/ch3/Makefile b/idiomatic_go/ch3/Makefile similarity index 100% rename from ch3/Makefile rename to idiomatic_go/ch3/Makefile diff --git a/ch3/go.mod b/idiomatic_go/ch3/go.mod similarity index 100% rename from ch3/go.mod rename to idiomatic_go/ch3/go.mod diff --git a/ch3/my_slice b/idiomatic_go/ch3/my_slice similarity index 100% rename from ch3/my_slice rename to idiomatic_go/ch3/my_slice diff --git a/ch3/slice.go b/idiomatic_go/ch3/slice.go similarity index 100% rename from ch3/slice.go rename to idiomatic_go/ch3/slice.go diff --git a/ch5/README.md b/idiomatic_go/ch5/README.md similarity index 100% rename from ch5/README.md rename to idiomatic_go/ch5/README.md diff --git a/ch5/func_thing.go b/idiomatic_go/ch5/func_thing.go similarity index 100% rename from ch5/func_thing.go rename to idiomatic_go/ch5/func_thing.go diff --git a/ch5/func_thing_test.go b/idiomatic_go/ch5/func_thing_test.go similarity index 100% rename from ch5/func_thing_test.go rename to idiomatic_go/ch5/func_thing_test.go diff --git a/ch5/go.mod b/idiomatic_go/ch5/go.mod similarity index 100% rename from ch5/go.mod rename to idiomatic_go/ch5/go.mod diff --git a/learn_go_with_tests/helloworld/go.mod b/learn_go_with_tests/helloworld/go.mod new file mode 100644 index 0000000..f93f903 --- /dev/null +++ b/learn_go_with_tests/helloworld/go.mod @@ -0,0 +1,3 @@ +module hello + +go 1.21.0 diff --git a/learn_go_with_tests/helloworld/hello.go b/learn_go_with_tests/helloworld/hello.go new file mode 100644 index 0000000..e5e584e --- /dev/null +++ b/learn_go_with_tests/helloworld/hello.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +// Hello prints the string "Hello, world" +func Hello(name string) string { + return "Hello, " + name +} + +func main() { + fmt.Println(Hello("world")) +} diff --git a/learn_go_with_tests/helloworld/hello_test.go b/learn_go_with_tests/helloworld/hello_test.go new file mode 100644 index 0000000..2ea7cbe --- /dev/null +++ b/learn_go_with_tests/helloworld/hello_test.go @@ -0,0 +1,12 @@ +package main + +import "testing" + +func TestHello(t *testing.T) { + got := Hello("Drew") + expected := "Hello, Drew" + + if got != expected { + t.Errorf("got %q, expected %q", got, expected) + } +} diff --git a/scratch_space/request.go b/scratch_space/request.go new file mode 100644 index 0000000..3aef506 --- /dev/null +++ b/scratch_space/request.go @@ -0,0 +1,36 @@ +package main + +import ( + "bufio" + "fmt" + "io/ioutil" + "net/http" + "os" + "strings" +) + +func main() { + reader := bufio.NewReader(os.Stdin) + fmt.Print("Enter request URL: ") + url, err := reader.ReadString('\n') + if err != nil { + fmt.Println("Error:", err) + return + } + url = strings.TrimSpace(url) + + response, err := http.Get(url) + if err != nil { + fmt.Println("Error:", err) + return + } + defer response.Body.Close() + + body, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error:", err) + return + } + + fmt.Println(string(body)) +}