diff --git a/README.md b/README.md index 9649cf0..01adf7c 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,14 @@ Following the O'Reilly "Learning Go: An Idiomatic Approach to Real-World Go Prog - The use of `./...` denotes a wildcard for tools like `go fmt`. It matches all .go files in the current directory and it's subdirectories. +## Installing Revive + +The legacy linter I guess is no longer supported but the [revive](https://revive.run/docs#installation) project is a reasonable successor. + +``` +go install github.com/mgechev/revive@latest +``` + +## Go Workspaces + +Ok I don't really know about modules and workspaces, but to quiet the IDE I needed to add a `go.work` file to the root here, then a `go.mod` file to each subdir. We can figure this out later. diff --git a/ch1/hello.go b/ch1/hello.go index 6b5e726..b0d739c 100644 --- a/ch1/hello.go +++ b/ch1/hello.go @@ -6,5 +6,5 @@ package main import "fmt" func main() { - fmt.Println("Hello Golang!") + fmt.Println("Hello world!") } diff --git a/ch3/Makefile b/ch3/Makefile new file mode 100644 index 0000000..87db7a6 --- /dev/null +++ b/ch3/Makefile @@ -0,0 +1,21 @@ +.DEFAULT_GOAL := build + +fmt: + go fmt ./... +.PHONY:fmt + +lint: fmt + revive ./... +.PHONY:lint + +vet: fmt + go vet ./... +.PHONY:vet + +build: vet + go build -o my_slice slice.go +.PHONY:build + +clean: + rm rf ./my_slice +.PHONY:clean diff --git a/ch3/go.mod b/ch3/go.mod new file mode 100644 index 0000000..ac7eea2 --- /dev/null +++ b/ch3/go.mod @@ -0,0 +1,3 @@ +module ch3 + +go 1.21 diff --git a/ch3/my_slice b/ch3/my_slice new file mode 100755 index 0000000..0b37afe Binary files /dev/null and b/ch3/my_slice differ diff --git a/ch3/slice.go b/ch3/slice.go new file mode 100644 index 0000000..e8deea7 --- /dev/null +++ b/ch3/slice.go @@ -0,0 +1,40 @@ +// Illustrates how slices capacity will increase as elements are added. +package main + +import "fmt" + +func main() { + var data = []int{1, 2, 3} + fmt.Println(data, len(data), cap(data)) + data = append(data, 10) + fmt.Println(data, len(data), cap(data)) + data = append(data, 20) + fmt.Println(data, len(data), cap(data)) + data = append(data, 30) + fmt.Println(data, len(data), cap(data)) + data = append(data, 40) + fmt.Println(data, len(data), cap(data)) + + //creates a slice of 3 ints using slice literal. + // specifies the indices with values in the slice literal + var x = []int{1, 5: 4, 6, 10: 100, 15} + // [1 0 0 0 0 4 6 0 0 0 100 15] + fmt.Println(x) + + // multi-dimensional slice "simulatation" + var y [][]int + fmt.Println(y) + //reading and writing using bracket syntax + y = append(y, []int{1, 2, 3}) + fmt.Println(y, len(y), cap(y)) + fmt.Println(y[0]) + + // Forloop + + // we can us := inside of functions not in globals of module + numbers := []int{1, 3, 5, 7, 9} + + for i := 0; i < len(numbers); i++ { + fmt.Println(numbers[i]) + } +} diff --git a/ch5/README.md b/ch5/README.md new file mode 100644 index 0000000..92bf515 --- /dev/null +++ b/ch5/README.md @@ -0,0 +1,8 @@ +# Functions + +## Unit Tests + +Ok from what I can tell a file for unit tests should end in `_test.go` matching the file name it is testing (covention?) Tests of functions +should begin with `TestFuncNameOrTestScenario(t *testing.T)`. The `t *testing.T` is pointer to a testing.T object which is a struct with that provides methods for writing unit tests. + +Then you use `go test` to run the unit tests. \ No newline at end of file diff --git a/ch5/func_thing.go b/ch5/func_thing.go new file mode 100644 index 0000000..4f7e708 --- /dev/null +++ b/ch5/func_thing.go @@ -0,0 +1,5 @@ +package main + +func AddStuff(a int, b int) int { + return a + b +} diff --git a/ch5/func_thing_test.go b/ch5/func_thing_test.go new file mode 100644 index 0000000..12b47b0 --- /dev/null +++ b/ch5/func_thing_test.go @@ -0,0 +1,13 @@ +package main + +import "testing" + +// The *testing.T syntax is used in Golang unit tests because it allows you to access the methods of the testing.T object without having to dereference the pointer. This makes the code more concise and easier to read. +func TestAddStuff(t *testing.T) { + expected := 3 + actual := AddStuff(1, 3) + + if expected != actual { + t.Errorf("Expected %d, got %d", expected, actual) + } +} diff --git a/ch5/go.mod b/ch5/go.mod new file mode 100644 index 0000000..6e9c63c --- /dev/null +++ b/ch5/go.mod @@ -0,0 +1,3 @@ +module ch5 + +go 1.21.0 diff --git a/coursera/week2/findian.go b/coursera/week2/findian.go new file mode 100644 index 0000000..49bce03 --- /dev/null +++ b/coursera/week2/findian.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "strings" +) + +func main() { + var userInput string + fmt.Println("Please submit a string and I will check if it begins with 'i', ends in 'n' and contains an 'a':") + fmt.Scan(&userInput) + + if strings.HasPrefix(userInput, "i") && strings.HasSuffix(userInput, "n") && strings.Contains(userInput, "a") { + fmt.Println("Found!") + } else { + fmt.Println("Not Found!") + } +} diff --git a/coursera/week2/trunc.go b/coursera/week2/trunc.go new file mode 100644 index 0000000..70eb7c5 --- /dev/null +++ b/coursera/week2/trunc.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "os" + "strconv" +) + +const exitCommand = "/q" + +func main() { + var userInput string + fmt.Printf("Welcome to trunc.go. Please enter %s to exit the program.\n", exitCommand) + for { + fmt.Println("Enter a float number:") + fmt.Scan(&userInput) + + if userInput == exitCommand { + fmt.Println("Closing...") + os.Exit(0) + } + + userFloat, err := strconv.ParseFloat(userInput, 64) + if err != nil { + fmt.Println("Opps you entered and incorrect input! Try again.") + continue + } + fmt.Printf("You entered: %f, the interger component of that is: %d\n", userFloat, int(userFloat)) + } +} diff --git a/coursera/week3/array.go b/coursera/week3/array.go new file mode 100644 index 0000000..54197bb --- /dev/null +++ b/coursera/week3/array.go @@ -0,0 +1,31 @@ +package main + +import "fmt" + +var myArray [5]int +var myArrayLiteral = [...]int{1, 2, 3, 3, 5} + +func main() { + fmt.Println(myArray) + myArray[0] = 100 + fmt.Println(myArray) + myArray[len(myArray)-1] = 1000 + fmt.Println(myArray) + fmt.Println(myArrayLiteral) + + // iterating through an array + for i := 0; i < len(myArrayLiteral); i++ { + fmt.Println(myArrayLiteral[i]) + } + + // iterate in reverse + for i := len(myArrayLiteral) - 1; i >= 0; i-- { + fmt.Println(myArrayLiteral[i]) + } + + // EVEN BETTER + for i, v := range myArrayLiteral { + fmt.Printf("index: %d, value: %d\n", i, v) + } + +} diff --git a/coursera/week3/maps.go b/coursera/week3/maps.go new file mode 100644 index 0000000..439f3e2 --- /dev/null +++ b/coursera/week3/maps.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +var myArray [5]int + +func main() { + fmt.Println(myArray) + myArray[0] = 100 + fmt.Println(myArray) + myArray[len(myArray)-1] = 1000 + fmt.Println(myArray) +} diff --git a/coursera/week3/slice.go b/coursera/week3/slice.go new file mode 100644 index 0000000..e69de29