lists
This commit is contained in:
3
lists-practice/go.mod
Normal file
3
lists-practice/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module example.com/lists-practice
|
||||
|
||||
go 1.23.1
|
||||
54
lists-practice/lists.go
Normal file
54
lists-practice/lists.go
Normal file
@@ -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)
|
||||
}
|
||||
3
lists/go.mod
Normal file
3
lists/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module example.com/lists
|
||||
|
||||
go 1.23.1
|
||||
33
lists/main.go
Normal file
33
lists/main.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
18
lists/maps/maps.go
Normal file
18
lists/maps/maps.go
Normal file
@@ -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)
|
||||
}
|
||||
35
lists/slices/lists.go
Normal file
35
lists/slices/lists.go
Normal file
@@ -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))
|
||||
// }
|
||||
@@ -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) {
|
||||
|
||||
38
structs-practice/todo/todo.go
Normal file
38
structs-practice/todo/todo.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user