add coursera

drew/sql-it
Drew Bednar 1 year ago
parent f88e88b406
commit 9f1bb5ddc0

@ -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.

@ -6,5 +6,5 @@ package main
import "fmt"
func main() {
fmt.Println("Hello Golang!")
fmt.Println("Hello world!")
}

@ -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

@ -0,0 +1,3 @@
module ch3
go 1.21

Binary file not shown.

@ -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])
}
}

@ -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.

@ -0,0 +1,5 @@
package main
func AddStuff(a int, b int) int {
return a + b
}

@ -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)
}
}

@ -0,0 +1,3 @@
module ch5
go 1.21.0

@ -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!")
}
}

@ -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))
}
}

@ -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)
}
}

@ -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)
}
Loading…
Cancel
Save