structs
This commit is contained in:
3
structs/go.mod
Normal file
3
structs/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module example.com/structs
|
||||
|
||||
go 1.21.2
|
||||
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