Files
purpleschool/go-demo-4/account/account.go
2025-03-05 00:33:09 +03:00

74 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package account
import (
"errors"
"fmt"
"math/rand/v2"
"net/url"
"time"
)
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGIJKLMNOPQRSTUVWXYZ1234567890-*!?()#$%&")
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)
}
// метод генерации пароля
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 PromptData(prompt string) string {
fmt.Print(prompt)
var res string
fmt.Scanln(&res)
return res
}