Check if an e-mail address is valid

With net/mail.

func validateEmailAddress(email string) bool {
    _, err := mail.ParseAddress(email)
    if err != nil {
        return false
    }
    return true
}

Generate UUID according to RFC 4122

With crypto/rand.

func NewUUID() (string, error) {
    uuid := make([]byte, 16)
    n, err := io.ReadFull(rand.Reader, uuid)
    if n != len(uuid) || err != nil {
        return "", err
    }
    uuid[8] = uuid[8]&^0xc0 | 0x80
    uuid[6] = uuid[6]&^0xf0 | 0x40
    return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}

Hash a password and Compare

With golang.org/x/crypto/bcrypt.

// Computes the hash
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)

// Compares the hash with the given password
err := bcrypt.CompareHashAndPassword(hash, []byte(password)
if err != nil {
    fmt.Println("Passwords do not match")
}

Scan SQL result in struct

With database/sql.

type User struct {
    Email            string
    password         string
    ID               string
}

user := new(User)
err := db.QueryRow("SELECT id, email, password FROM users WHERE email = $1",
    email,
).Scan(&user.ID, &user.Email, &user.password)