Created
August 9, 2012 09:33
-
-
Save leejarvis/3302678 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
_ "github.com/bmizerany/pq" | |
"database/sql" | |
"log" | |
"fmt" | |
"encoding/json" | |
) | |
type User struct { | |
ID int `json:"id"` | |
Name string `json:"name"` | |
} | |
var users []*User | |
func main() { | |
db, err := sql.Open("postgres", "user=lee dbname=allur_development sslmode=disable") | |
defer db.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
r, err := db.Query("SELECT id, firstname FROM users WHERE firstname IS NOT NULL ORDER BY id") | |
defer r.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
for r.Next() { | |
user := new(User) | |
r.Scan(&user.ID, &user.Name) | |
users = append(users, user) | |
} | |
for _, u := range users { | |
json, err := json.Marshal(u) | |
if err == nil { | |
fmt.Println(string(json)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment