Add struct, method, construct, composition

This commit is contained in:
Archie Fox
2025-03-04 00:55:21 +03:00
parent 850430b62e
commit 13868f6a8a
2 changed files with 116 additions and 10 deletions

View File

@@ -1,20 +1,106 @@
package main
import (
"errors"
"fmt"
"math/rand/v2"
"net/url"
"time"
)
type account struct {
login string
password string
url string
}
type accountWithTimeStamp struct {
createdAt time.Time
updatedAt time.Time
account
}
// метод вывода пароля
func (acc account) outputPassword() {
fmt.Println(acc.login, acc.password, acc.url)
}
func (acc accountWithTimeStamp) outputPassword() {
fmt.Println(acc.login, acc.password, acc.url, acc.createdAt, acc.updatedAt)
}
// метод генерации пароля
func (acc *account) generatePassword(n int) {
res := make([]rune, n)
for i := range res {
res[i] = letterRunes[rand.IntN(len(letterRunes))]
}
acc.password = string(res)
}
// Создание аккаунта с таймстамп
func newAccountWithTimeStamp(login, password, urlString string) (*accountWithTimeStamp, error) {
_, err := url.ParseRequestURI(urlString)
if err != nil {
return nil, errors.New("INVALID_URL")
}
if login == "" {
return nil, errors.New("INVALID_LOGIN")
}
newAcc := &accountWithTimeStamp{
createdAt: time.Now(),
updatedAt: time.Now(),
account: account{
login: login,
password: password,
url: urlString,
},
}
if password == "" {
newAcc.generatePassword(18)
}
return newAcc, nil
}
// создание аккаунта
/* func newAccount(login, password, urlString string) (*account, error) {
_, err := url.ParseRequestURI(urlString)
if err != nil {
return nil, errors.New("INVALID_URL")
}
if login == "" {
return nil, errors.New("INVALID_LOGIN")
}
newAcc := &account{
login: login,
password: password,
url: urlString,
}
if password == "" {
newAcc.generatePassword(18)
}
return newAcc, nil
} */
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGIJKLMNOPQRSTUVWXYZ1234567890-*!?()#$%&")
func main() {
a := [4]int{1, 2, 3, 4}
reverse(&a)
fmt.Println(a)
login := promptData("Введите логин: ")
password := promptData("Введите пароль: ")
url := promptData("Введите URL: ")
var n *int
fmt.Println(*n)
myAccount, err := newAccountWithTimeStamp(login, password, url)
if err != nil {
fmt.Println("ОШИБКА: Неверный формат URL")
return
}
myAccount.outputPassword()
}
func reverse(arr *[4]int) {
for idx, val := range *arr {
(*arr)[len(arr)-1-idx] = val
}
// функция введения данных
func promptData(prompt string) string {
fmt.Print(prompt)
var res string
fmt.Scanln(&res)
return res
}

View File

@@ -0,0 +1,20 @@
package main
import (
"fmt"
)
func main() {
a := [4]int{1, 2, 3, 4}
reverse(&a)
fmt.Println(a)
// var n *int
// fmt.Println(*n)
}
func reverse(arr *[4]int) {
for idx, val := range *arr {
(*arr)[len(arr)-1-idx] = val
}
}