Snippets: Golang: URL

 17th December 2020 at 5:06pm

通过 net/url 包可以做 URL 的解析和生成。url 库遵循 RFC 3986,所以像 PG 的 connection string 也可以通过这种方式构建:

import (
	"fmt"
	"net/url"
)

func main() {
	u := &url.URL{
		Scheme:   "postgresql",
		User:     url.UserPassword("squirrel", "iloveice"),
		Host:     "localhost",
		Path:     "squirrel",
	}
	fmt.Println(u.String())
}