• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang pq.ParseURL函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/bmizerany/pq.ParseURL函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseURL函数的具体用法?Golang ParseURL怎么用?Golang ParseURL使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了ParseURL函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: getdb

func getdb() *sql.DB {
	var settings, herokupg, localpg string
	herokupg = os.Getenv("DATABASE_URL")
	if herokupg != "" {
		settings, _ = pq.ParseURL(herokupg)
	} else {
		c, _ := goconf.ReadConfigFile("db.config")
		localpg, _ = c.GetString("dev", "postgresurl")
		settings, _ = pq.ParseURL(localpg)
	}
	db, _ := sql.Open("postgres", settings)
	return db
}
开发者ID:stehem,项目名称:plenty_of_links,代码行数:13,代码来源:plenty.go


示例2: main

func main() {
	flag.Parse()
	cs, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Println("unable to parse database url")
		os.Exit(1)
	}

	pg, err = sql.Open("postgres", cs)
	if err != nil {
		log.Println("unable to connect to database")
		os.Exit(1)
	}

	log.Println("bind tcp", os.Getenv("PORT"))
	server, err := net.Listen("tcp", "0.0.0.0:"+os.Getenv("PORT"))
	if err != nil {
		log.Println("unable to bind tcp")
		os.Exit(1)
	}
	conns := clientConns(server)
	for {
		go readData(<-conns)
	}
}
开发者ID:ryandotsmith,项目名称:wcld,代码行数:25,代码来源:wcld.go


示例3: connectToDB

func connectToDB() {
	var err error
	dataSource, err := pq.ParseURL(os.Getenv("PGURL"))
	handleError(err, "URL PARSE")
	DB, err = sql.Open("postgres", dataSource)
	handleError(err, "DB CONNECT")
}
开发者ID:minhajuddin,项目名称:httptoemail,代码行数:7,代码来源:db.go


示例4: feel

func feel(databaseUrl string) {
	dataSource, err := pq.ParseURL(databaseUrl)
	if err != nil {
		fmt.Printf("Unable to parse database url (%s)", databaseUrl)
		panic(err)
	}
	u, err := url.Parse(databaseUrl)
	if err != nil {
		panic(err)
	}
	databaseName := u.Path[1:]
	db, err := sql.Open("postgres", dataSource)
	if err != nil {
		fmt.Println("Unable to connect to database")
	}

	for {
		row := db.QueryRow("SELECT count(*) from pg_stat_activity")
		var count uint16
		err = row.Scan(&count)
		if err != nil {
			panic(err)
		}
		row = db.QueryRow("SELECT pg_database_size($1)", databaseName)
		var bytes uint64
		err = row.Scan(&bytes)
		if err != nil {
			panic(err)
		}
		o := observation{connections: count, databaseUrl: databaseUrl, bytes: bytes, serviceAvailable: true, updatedAt: time.Now()}
		o.persist()
	}
}
开发者ID:hgmnz,项目名称:observatory,代码行数:33,代码来源:feel.go


示例5: initwebdb

func initwebdb() {
	connstr, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Fatal("pq.ParseURL", err)
	}
	db, err = sql.Open("postgres", connstr+" sslmode=disable")
	if err != nil {
		log.Fatal("sql.Open", err)
	}
	mustExec(`SET bytea_output = 'hex'`) // work around https://github.com/bmizerany/pq/issues/76
	mustExec(`create table if not exists release (
		plat text not null,
		cmd text not null,
		ver text not null,
		sha256 bytea not null,
		primary key (plat, cmd, ver)
	)`)
	mustExec(`create table if not exists cur (
		plat text not null,
		cmd text not null,
		curver text not null,
		foreign key (plat, cmd, curver) references release (plat, cmd, ver),
		primary key (plat, cmd)
	)`)
	mustExec(`create table if not exists mod (
		t timestamptz not null
	)`)
	mustExec(`insert into mod (t)
		select now()
		where not exists (select 1 from mod)
	`)
}
开发者ID:herokai,项目名称:hk,代码行数:32,代码来源:web.go


示例6: db_open

func db_open(url string) (*sql.DB, error) {
	conn_str, err := pq.ParseURL(url)
	if err != nil {
		return nil, err
	}
	return sql.Open("postgres", conn_str)
}
开发者ID:Khady,项目名称:obiwan-kanbanobi,代码行数:7,代码来源:db_utils.go


示例7: dbConnect

func dbConnect() (db *sql.DB, err error) {
	dbUrl := os.Getenv("DATABASE_URL")
	source, err := pq.ParseURL(dbUrl)
	if err != nil {
		db, err = sql.Open("postgres", source)
	}
	return
}
开发者ID:janne,项目名称:gometrics,代码行数:8,代码来源:input.go


示例8: storeBatches

func storeBatches(batches <-chan []LogMessage) {
	conn, err := pg.ParseURL(os.Getenv("DATABASE_URL"))

	if err != nil {
		panic(err)
	}

	db, err := sql.Open("postgres", conn)

	if err != nil {
		panic(err)
	}

	_, err = db.Query("SET application_name TO \"log-stuff:storeBatches\";")

	if err != nil {
		panic(err)
	}

	insertStatement, err := db.Prepare("INSERT INTO logs values ($1, $2, $3, $4, $5, $6, $7, $8)")

	if err != nil {
		panic(err)
	}

	rows_inserted := 0

	for batch := range batches {
		tx, err := db.Begin()

		if err != nil {
			panic(err)
		}

		txStmt := tx.Stmt(insertStatement)

		for _, item := range batch {
			_, err := txStmt.Exec(item.facility, item.severity, item.length, item.timestamp, item.drain, item.source, item.process, item.log)
			if err != nil {
				panic(err)
			}
			rows_inserted += 1
		}

		err = tx.Commit()

		if err != nil {
			panic(err)
		}

		rows_inserted = 0
	}
}
开发者ID:freeformz,项目名称:log_stuff,代码行数:53,代码来源:main.go


示例9: openTestDB

func openTestDB(t *testing.T) *sql.DB {
	cs, err := pq.ParseURL("postgres://localhost:5432/gls_test")
	if err != nil {
		t.Fatal(err)
	}
	cs += " sslmode=disable"
	db, err := OpenDB(cs)
	if err != nil {
		t.Fatal(err)
	}
	return db
}
开发者ID:bgentry,项目名称:gls,代码行数:12,代码来源:db_test.go


示例10: init

func init() {
	url := os.Getenv("DATABASE_URL")
	if len(url) == 0 {
		postgresEnabled = false
		fmt.Println("Postgres has been disabled.")
		return
	}
	str, err := pq.ParseURL(url)
	if err != nil {
		fmt.Printf("error=\"unable to parse DATABASE_URL\"\n")
		os.Exit(1)
	}
	pg, err = sql.Open("postgres", str)
	if err != nil {
		fmt.Printf("error=%s\n", err)
		os.Exit(1)
	}

	rurl := os.Getenv("DATABASE_READ_URL")
	if len(rurl) > 0 {
		rstr, err := pq.ParseURL(rurl)
		if err != nil {
			fmt.Printf("error=\"unable to parse DATABASE_READ_URL\"\n")
			os.Exit(1)
		}
		pgRead, err = sql.Open("postgres", rstr)
		if err != nil {
			fmt.Printf("error=%s\n", err)
			os.Exit(1)
		}
		return
	}

	fmt.Printf("Missing DATABASE_READ_URL. Using DATABASE_URL to service reads.\n")
	pgRead, err = sql.Open("postgres", str)
	if err != nil {
		fmt.Printf("error=%s\n", err)
		os.Exit(1)
	}
}
开发者ID:Epictetus,项目名称:l2met,代码行数:40,代码来源:pg.go


示例11: init

func init() {
	url := os.Getenv("DATABASE_URL")
	if len(url) == 0 {
		fmt.Printf("at=error error=\"must set DATABASE_URL\"\n")
		os.Exit(1)
	}
	str, err := pq.ParseURL(url)
	if err != nil {
		fmt.Printf("at=error error=\"unable to parse DATABASE_URL\"\n")
		os.Exit(1)
	}
	PG, err = sql.Open("postgres", str)
	if err != nil {
		fmt.Printf("at=error error=%s\n", err)
		os.Exit(1)
	}

	rurl := os.Getenv("DATABASE_READ_URL")
	if len(rurl) > 0 {
		rstr, err := pq.ParseURL(rurl)
		if err != nil {
			fmt.Printf("at=error error=\"unable to parse DATABASE_READ_URL\"\n")
			os.Exit(1)
		}
		PGR, err = sql.Open("postgres", rstr)
		if err != nil {
			fmt.Printf("at=error error=%s\n", err)
			os.Exit(1)
		}
		return
	}

	fmt.Printf("Missing DATABASE_READ_URL. Using DATABASE_URL to service reads.\n")
	PGR, err = sql.Open("postgres", str)
	if err != nil {
		fmt.Printf("at=error error=%s\n", err)
		os.Exit(1)
	}
}
开发者ID:freeformz,项目名称:l2met,代码行数:39,代码来源:pg.go


示例12: dbConnect

func dbConnect() *sql.DB {
	connStr, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
	if err != nil {
		panic(err)
	}

	db, err := sql.Open("postgres", connStr)
	if err != nil {
		fmt.Printf("Error connecting: %s", err)
	}

	return db
}
开发者ID:OllyGinger,项目名称:ImgurGallery,代码行数:13,代码来源:imgurgallery.go


示例13: GetConn

func GetConn() *sql.DB {
	// Get the connection string from the DATABASE_URL env var
	connString, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
	if err != nil {
		panic(err)
	}

	db, err := sql.Open("postgres", connString+" sslmode=disable")

	if err != nil {
		panic(err)
	}

	return db
}
开发者ID:bretthop,项目名称:link-board-heroku,代码行数:15,代码来源:ConnectionManager.go


示例14: parseUrl

func parseUrl(name string) string {
	var tmp string
	var err error

	tmp = os.Getenv(name)
	if len(tmp) == 0 {
		fmt.Printf("at=error error=\"must set %s\"\n", name)
		os.Exit(1)
	}

	tmp, err = pq.ParseURL(tmp)
	if err != nil {
		fmt.Printf("at=error error=\"unable to parse %s\"\n", name)
		os.Exit(1)
	}
	return tmp
}
开发者ID:ryandotsmith,项目名称:komrade,代码行数:17,代码来源:pg.go


示例15: getDatabaseUrl

// Get the Postgres Database Url.
func getDatabaseUrl() string {
	var database_url = os.Getenv("DATABASE_URL")
	// Set a default database url if there is nothing in the environemnt
	if database_url == "" {
		// Postgres.app uses this variable to set-up postgres.
		user := os.Getenv("USER")
		// Inject the username into the connection string
		database_url = "postgres://" + user + "@localhost/up?sslmode=disable"
		// Let the user know they are using a default
		fmt.Println("INFO: No DATABASE_URL env var detected, defaulting to " + database_url)
	}
	conn_str, err := pq.ParseURL(database_url)
	if err != nil {
		panic("Unable to Parse DB URL connection string: " + database_url)
	}
	return conn_str
}
开发者ID:pearkes,项目名称:up-server,代码行数:18,代码来源:config.go


示例16: init

func init() {
	fmt.Println("connecting")
	url := os.Getenv("DATABASE_URL")
	if len(url) == 0 {
		fmt.Printf("error=\"must set DATABASE_URL\"\n")
		os.Exit(1)
	}
	str, err := pq.ParseURL(url)
	if err != nil {
		fmt.Printf("error=\"unable to parse DATABASE_URL\"\n")
		os.Exit(1)
	}
	pg, err = sql.Open("postgres", str)
	if err != nil {
		fmt.Printf("error=%s\n", err)
		os.Exit(1)
	}
}
开发者ID:brandur,项目名称:blackswan-go,代码行数:18,代码来源:pg.go


示例17: getConnection

func getConnection() *sql.DB {
	config, err := toml.LoadFile("config.toml")
	checkError(err)
	configTree := config.Get("postgres").(*toml.TomlTree)
	user := configTree.Get("user").(string)
	password := configTree.Get("password").(string)
	host := configTree.Get("host").(string)
	database := configTree.Get("database").(string)
	port := configTree.Get("port").(int64)

	rawUrl := fmt.Sprintf(
		"postgres://%s:%[email protected]%s:%d/%s?sslmode=disable",
		user, password, host, port, database,
	)
	url, err := pq.ParseURL(rawUrl)
	checkError(err)
	db, err := sql.Open("postgres", url)
	checkError(err)
	return db
}
开发者ID:kevinburke,项目名称:snapchat-friends,代码行数:20,代码来源:db.go


示例18: main

func main() {
	args := os.Args
	if len(args) != 2 {
		conn_example := "postgres://user:[email protected]:port/db"
		fmt.Println("Please provide a connection string in this format",
			conn_example)
		return
	}

	conn, err := pq.ParseURL(args[1])
	if err != nil {
		fmt.Println("Wrong format of connect string.")
		return
	}

	db, openerr := OpenDB(conn)
	if openerr != nil {
		fmt.Println("DB open error", openerr)
		return
	}

	defer db.Close()

	var i int

	row := db.QueryRow("SELECT 1")
	err_ := row.Scan(&i)

	if err_ != nil {
		fmt.Println(err_)
		return
	}

	if i != 1 {
		fmt.Println("'SELECT 1' expects 1, but we got", i)
		return
	}

	fmt.Println("Great, 'SELECT 1' returns", i)
	return
}
开发者ID:wujiang,项目名称:goe,代码行数:41,代码来源:simple_connect.go


示例19: init

func init() {
	url := os.Getenv("DATABASE_URL")
	if len(url) == 0 {
		fmt.Printf("error=\"must set DATABASE_URL\"\n")
		os.Exit(1)
	}

	pgurl, err := pq.ParseURL(url)
	if err != nil {
		fmt.Printf("error=\"unable to parse DATABASE_URL\"\n")
		os.Exit(1)
	}

	pg, err = sql.Open("postgres", pgurl)
	if err != nil {
		fmt.Printf("error=%s\n", err)
		os.Exit(1)
	}

	pg.Exec("set application_name = 'l2met-next_librato'")
}
开发者ID:ericfode,项目名称:l2met,代码行数:21,代码来源:pg.go


示例20: Open

func Open(url string) (*sql.DB, error) {
	if url == "" {
		url = os.Getenv("DATABASE_URL")
	}

	if url == "" {
		url = os.Getenv("BOXEN_POSTGRESQL_URL")
		if url != "" {
			url += "simplex?sslmode=disable"
		}
	}

	src, err := pq.ParseURL(url)
	if err != nil {
		return nil, err
	}

	db, err := sql.Open("postgres", src)
	if err != nil {
		return nil, err
	}

	return db, nil
}
开发者ID:fd,项目名称:simplex,代码行数:24,代码来源:open.go



注:本文中的github.com/bmizerany/pq.ParseURL函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang crash.Info类代码示例发布时间:2022-05-24
下一篇:
Golang quantile.NewTargeted函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap