Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "math/rand"
- "fmt"
- "database/sql"
- "time"
- _ "github.com/lib/pq"
- )
- const (
- DB_USER = "postgres"
- DB_PASSWORD = "postgres"
- DB_NAME = "testdb"
- )
- func randomTimestamp() time.Time {
- randomTime := rand.Int63n(time.Now().Unix())
- randomNow := time.Unix(randomTime, 0)
- return randomNow
- }
- func randomString() string {
- const charset = "abcdefghijklmnopqrstuvwxyz" +
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- b := make([]byte, rand.Intn(50))
- for i := range b {
- b[i] = charset[rand.Intn(len(charset))]
- }
- return string(b)
- }
- func main() {
- dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
- DB_USER, DB_PASSWORD, DB_NAME)
- db, err := sql.Open("postgres", dbinfo)
- if err != nil {
- panic(err)
- }
- defer db.Close()
- for i:= 0; i < 10; i++ {
- name := randomString()
- time := randomTimestamp().Format("2006-01-02 15:04:05")
- author := randomString()
- db.QueryRow("insert into post(id, name, date, author) values(DEFAULT, $1, $2, $3);", name, time, author)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement