Created
May 1, 2021 11:41
-
-
Save utkarshmani1997/a530c1437031492a665c8eb063734649 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package database | |
const ( | |
insertQuery = ` | |
INSERT INTO users (age, email, first_name, last_name) | |
VALUES ($1, $2, $3, $4) | |
RETURNING id | |
` | |
) | |
type Users interface { | |
Insert() (int, error) | |
} | |
type User struct { | |
Age int | |
Email string | |
FirstName string | |
LastName string | |
} | |
func NewUser(age int, email, first, last string) Users { | |
return User{age, email, first, last} | |
} | |
func (u User) Insert() (int, error) { | |
id := 0 | |
err := db.QueryRow(insertQuery, u.Age, u.Email, u.FirstName, u.LastName).Scan(&id) | |
return id, err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment