Created
May 22, 2014 11:00
-
-
Save reterVision/5ce0aebe45ce3b91da0c to your computer and use it in GitHub Desktop.
Load Django User table information from PostgreSQL in Go
This file contains 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
// | |
// go get github.com/lib/pq | |
// | |
package main | |
import ( | |
_ "github.com/lib/pq" | |
"database/sql" | |
"fmt" | |
"log" | |
"time" | |
) | |
type User struct { | |
Id int | |
Username string | |
FirstName string | |
LastName string | |
Email string | |
Password string | |
IsStaff bool | |
IsActive bool | |
IsSuperuser bool | |
LastLogin time.Time | |
DateJoined time.Time | |
} | |
func main() { | |
db, err := sql.Open("postgres", "user=test dbname=djangotest sslmode=disable") | |
defer db.Close() | |
if err != nil { | |
fmt.Println("open error") | |
log.Fatal(err) | |
} | |
rows, err := db.Query("SELECT * FROM auth_user limit 1;") | |
if err != nil { | |
fmt.Println("select error") | |
log.Fatal(err) | |
} | |
for rows.Next() { | |
var user User | |
err = rows.Scan(&user.Id, | |
&user.Username, | |
&user.FirstName, &user.LastName, | |
&user.Email, &user.Password, | |
&user.IsStaff, &user.IsActive, &user.IsSuperuser, | |
&user.LastLogin, &user.DateJoined) | |
if err != nil { | |
fmt.Println("Scan error") | |
log.Fatal(err) | |
} | |
fmt.Println(user) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment