structs
This commit is contained in:
3
investmentCalculator/go.mod
Normal file
3
investmentCalculator/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module example.com/investment-calculator
|
||||
|
||||
go 1.23.1
|
||||
3
structs-practice/go.mod
Normal file
3
structs-practice/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module example.com/note
|
||||
|
||||
go 1.23.1
|
||||
55
structs-practice/main.go
Normal file
55
structs-practice/main.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"example.com/note/note"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
title, content := getNoteData()
|
||||
|
||||
userNote, err := note.New(title, content)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
userNote.Display()
|
||||
err = userNote.Save()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Saving the note failed")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Saving the note succeeded!")
|
||||
}
|
||||
|
||||
func getNoteData() (string, string) {
|
||||
title := getUserInput("Note title:")
|
||||
|
||||
content := getUserInput("Note content:")
|
||||
|
||||
return title, content
|
||||
}
|
||||
|
||||
func getUserInput(prompt string) (string) {
|
||||
fmt.Printf("%v ", prompt)
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
text, err :=reader.ReadString('\n')
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
text = strings.TrimSuffix(text, "\n")
|
||||
text = strings.TrimSuffix(text, "\r")
|
||||
|
||||
return text
|
||||
}
|
||||
45
structs-practice/note/note.go
Normal file
45
structs-practice/note/note.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Note struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (note Note) Display() {
|
||||
fmt.Printf("Your note title %v has the following content:\n\n%v", note.Title, note.Content)
|
||||
}
|
||||
|
||||
func (note Note) Save() error {
|
||||
fileName := strings.ReplaceAll(note.Title, " ", "_")
|
||||
fileName = strings.ToLower(fileName) + ".json"
|
||||
|
||||
json, err := json.Marshal(note)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(fileName, json, 0644)
|
||||
}
|
||||
|
||||
func New(title, content string) (Note, error) {
|
||||
if title == "" || content == "" {
|
||||
return Note{}, errors.New("Invalid input")
|
||||
}
|
||||
|
||||
return Note{
|
||||
Title: title,
|
||||
Content: content,
|
||||
CreatedAt: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
22
structs.go
22
structs.go
@@ -1,22 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
firstName := getUserData("Please enter your first name: ")
|
||||
lastName := getUserData("Please enter your last name: ")
|
||||
birthdate := getUserData("Please enter your birthdate (MM/DD/YYYY): ")
|
||||
|
||||
// ... do something awesome with that gathered data!
|
||||
|
||||
fmt.Println(firstName, lastName, birthdate)
|
||||
}
|
||||
|
||||
func getUserData(promptText string) string {
|
||||
fmt.Print(promptText)
|
||||
var value string
|
||||
fmt.Scan(&value)
|
||||
return value
|
||||
}
|
||||
40
structs/structs.go
Normal file
40
structs/structs.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"example.com/structs/user"
|
||||
)
|
||||
|
||||
func main() {
|
||||
userFirstName := getUserData("Please enter your first name: ")
|
||||
userLastName := getUserData("Please enter your last name: ")
|
||||
userBirthdate := getUserData("Please enter your birthdate (MM/DD/YYYY): ")
|
||||
|
||||
var appUser *user.User
|
||||
|
||||
appUser, err := user.New(userFirstName, userLastName, userBirthdate)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
admin := user.NewAdmin("test@example.com", "test123")
|
||||
|
||||
admin.OutputUserDetails()
|
||||
admin.ClearUserName()
|
||||
admin.OutputUserDetails()
|
||||
|
||||
// ... do something awesome with that gathered data!
|
||||
|
||||
appUser.OutputUserDetails()
|
||||
appUser.ClearUserName()
|
||||
appUser.OutputUserDetails()
|
||||
}
|
||||
|
||||
func getUserData(promptText string) string {
|
||||
fmt.Print(promptText)
|
||||
var value string
|
||||
fmt.Scanln(&value)
|
||||
return value
|
||||
}
|
||||
55
structs/user/user.go
Normal file
55
structs/user/user.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
firstName string
|
||||
lastName string
|
||||
birthdate string
|
||||
createdAt time.Time
|
||||
}
|
||||
|
||||
type Admin struct {
|
||||
email string
|
||||
password string
|
||||
User
|
||||
}
|
||||
|
||||
func (u User) OutputUserDetails() {
|
||||
fmt.Println(u.firstName, u.lastName, u.birthdate)
|
||||
}
|
||||
|
||||
func (u *User) ClearUserName() {
|
||||
u.firstName = ""
|
||||
u.lastName = ""
|
||||
}
|
||||
|
||||
func NewAdmin(email, password string) Admin {
|
||||
return Admin{
|
||||
email: email,
|
||||
password: password,
|
||||
User: User{
|
||||
firstName: "ADMIN",
|
||||
lastName: "ADMIN",
|
||||
birthdate: "---",
|
||||
createdAt: time.Now(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func New(firstName, lastName, birthdate string) (*User, error) {
|
||||
if firstName == "" || lastName == "" || birthdate == "" {
|
||||
return nil, errors.New("First name, last name, and birthdate are required")
|
||||
}
|
||||
|
||||
return &User{
|
||||
firstName: firstName,
|
||||
lastName: lastName,
|
||||
birthdate: birthdate,
|
||||
createdAt: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user