From a8148f24007a60b231e015e069a66252e65e3615 Mon Sep 17 00:00:00 2001 From: noahspannbauer Date: Wed, 23 Oct 2024 12:10:32 +0000 Subject: [PATCH] lists --- lists-practice/go.mod | 3 ++ lists-practice/lists.go | 54 +++++++++++++++++++++++++++++++++++ lists/go.mod | 3 ++ lists/main.go | 33 +++++++++++++++++++++ lists/maps/maps.go | 18 ++++++++++++ lists/slices/lists.go | 35 +++++++++++++++++++++++ structs-practice/main.go | 51 +++++++++++++++++++++++++++++---- structs-practice/todo/todo.go | 38 ++++++++++++++++++++++++ 8 files changed, 230 insertions(+), 5 deletions(-) create mode 100644 lists-practice/go.mod create mode 100644 lists-practice/lists.go create mode 100644 lists/go.mod create mode 100644 lists/main.go create mode 100644 lists/maps/maps.go create mode 100644 lists/slices/lists.go create mode 100644 structs-practice/todo/todo.go diff --git a/lists-practice/go.mod b/lists-practice/go.mod new file mode 100644 index 0000000..a72c5fd --- /dev/null +++ b/lists-practice/go.mod @@ -0,0 +1,3 @@ +module example.com/lists-practice + +go 1.23.1 diff --git a/lists-practice/lists.go b/lists-practice/lists.go new file mode 100644 index 0000000..be3bc4f --- /dev/null +++ b/lists-practice/lists.go @@ -0,0 +1,54 @@ +package main + +import "fmt" + +type Product struct { + id string + title string + price float64 +} + +func main() { + hobbies := [3]string{"Sports", "Cooking", "Reading"} + fmt.Println(hobbies) + + fmt.Println(hobbies[0]) + fmt.Println(hobbies[1:3]) + + mainHobbies := hobbies[:2] + fmt.Println(mainHobbies) + + fmt.Println(cap(mainHobbies)) + mainHobbies = mainHobbies[1:3] + fmt.Println(mainHobbies) + + courseGoals := []string{"Learn Go", "Learn all the basics"} + fmt.Println(courseGoals) + + courseGoals[1] = "Learn all the details" + courseGoals = append(courseGoals, "Learn all the stuff") + fmt.Println(courseGoals) + + products := []Product{ + { + "first-product", + "A first product", + 12.99, + }, + { + "second-product", + "A second product", + 129.99, + }, + } + + fmt.Println(products) + + newProduct := Product{ + "third-product", + "A third product", + 15.99, + } + + products = append(products, newProduct) +} \ No newline at end of file diff --git a/lists/go.mod b/lists/go.mod new file mode 100644 index 0000000..6ffbe05 --- /dev/null +++ b/lists/go.mod @@ -0,0 +1,3 @@ +module example.com/lists + +go 1.23.1 diff --git a/lists/main.go b/lists/main.go new file mode 100644 index 0000000..80e9440 --- /dev/null +++ b/lists/main.go @@ -0,0 +1,33 @@ +package main + +import "fmt" + +type floatMap map[string]float64 + +func (m floatMap) output() { + fmt.Println(m) +} + +func main() { + userNames := make([]string, 2, 5) + + userNames[0] = "Julie" + + userNames = append(userNames, "Max") + userNames = append(userNames, "Manuel") + + fmt.Println(userNames) + + courseRatings := make(floatMap, 3) + + courseRatings["go"] = 4.7 + courseRatings["react"] = 4.8 + courseRatings["angular"] = 4.7 + + courseRatings.output() + + for index, value := range userNames { + fmt.Println("Index:", index) + fmt.Println("Value:", value) + } +} \ No newline at end of file diff --git a/lists/maps/maps.go b/lists/maps/maps.go new file mode 100644 index 0000000..0a5ce6d --- /dev/null +++ b/lists/maps/maps.go @@ -0,0 +1,18 @@ +package maps + +import "fmt" + +func main() { + websites := map[string]string{ + "Google": "https://google.com", + "Amazon Web Services": "https://aws.com", + } + fmt.Println(websites) + fmt.Println(websites["Amazon Web Services"]) + + websites["LinkedIn"] = "https://linkedin.com" + fmt.Println(websites) + + delete(websites, "Google") + fmt.Println(websites) +} \ No newline at end of file diff --git a/lists/slices/lists.go b/lists/slices/lists.go new file mode 100644 index 0000000..1d5ac74 --- /dev/null +++ b/lists/slices/lists.go @@ -0,0 +1,35 @@ +package lists + +import "fmt" + +func main () { + prices := []float64{10.99, 8.99} + fmt.Println(prices[0:1]) + prices[1] = 9.99 + + prices = append(prices, 5.99, 12.99, 29.99, 100.10) + prices = prices[1:] + fmt.Println(prices) + + discountedPrices := []float64{101.99, 80.99, 20.99} + prices = append(prices, discountedPrices...) + fmt.Println(prices) +} + +// func main() { +// var productNames [4]string = [4]string{"A book"} +// prices := [4]float64{10.99, 9.99, 45.99, 20.0} +// fmt.Println(prices) +// fmt.Println(productNames) + +// productNames[2] = "A carpet" + +// fmt.Println(prices[2]) + +// featuredPrices := prices[1:] +// featuredPrices[0] = 199.99 +// highlightedPrices := featuredPrices[:1] +// fmt.Println(highlightedPrices) +// fmt.Println(prices) +// fmt.Println(len(highlightedPrices), cap(highlightedPrices)) +// } \ No newline at end of file diff --git a/structs-practice/main.go b/structs-practice/main.go index 30144fa..1dec158 100644 --- a/structs-practice/main.go +++ b/structs-practice/main.go @@ -2,31 +2,72 @@ package main import ( "bufio" - "fmt" "example.com/note/note" + "example.com/note/todo" + "fmt" "os" "strings" ) +type saver interface { + Save() error +} + +// type displayer interface { +// Display() +// } + +type outputtable interface { + saver + Display() +} + +// type outputtable interface { +// Save() error +// Display() +// } + func main() { title, content := getNoteData() + todoText := getUserInput("Todo text: ") - userNote, err := note.New(title, content) + todo, err := todo.New(todoText) if err != nil { fmt.Println(err) return } - userNote.Display() - err = userNote.Save() + userNote, err := note.New(title, content) if err != nil { - fmt.Println("Saving the note failed") return } + err = outputData(todo) + + if err != nil { + return + } + + outputData(userNote) +} + +func outputData(data outputtable) error { + data.Display() + return saveData(data) +} + +func saveData(data saver) error { + err := data.Save() + + if err != nil { + fmt.Println("Saving the note failed") + return err + } + fmt.Println("Saving the note succeeded!") + return nil } func getNoteData() (string, string) { diff --git a/structs-practice/todo/todo.go b/structs-practice/todo/todo.go new file mode 100644 index 0000000..ecf6f8a --- /dev/null +++ b/structs-practice/todo/todo.go @@ -0,0 +1,38 @@ +package todo + +import ( + "encoding/json" + "errors" + "fmt" + "os" +) + +type Todo struct { + Text string `json:"text"` +} + +func (todo Todo) Display() { + fmt.Printf(todo.Text) +} + +func (todo Todo) Save() error { + fileName := "todo.json" + + json, err := json.Marshal(todo) + + if err != nil { + return err + } + + return os.WriteFile(fileName, json, 0644) +} + +func New(content string) (Todo, error) { + if content == "" || content == "" { + return Todo{}, errors.New("Invalid input") + } + + return Todo{ + Text: content, + }, nil +} \ No newline at end of file