Skip to content

Commit

Permalink
URL 解析翻译完成
Browse files Browse the repository at this point in the history
  • Loading branch information
everyx committed Apr 26, 2014
1 parent 1374f9e commit 92541e2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ index 30d7291..e1caa03 100644
|Time Formatting / Parsing||
|Random Numbers||
|Number Parsing||
|URL Parsing||
|URL Parsing||
|SHA1 Hashes||
|Base64 Encoding||
|Reading Files||
Expand Down
40 changes: 18 additions & 22 deletions examples/url-parsing/url-parsing.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// URLs provide a [uniform way to locate resources](http://adam.heroku.com/past/2010/3/30/urls_are_the_uniform_way_to_locate_resources/).
// Here's how to parse URLs in Go.
// URL 提供了一个[统一资源定位方式](http://adam.heroku.com/past/2010/3/30/urls_are_the_uniform_way_to_locate_resources/)
// 这里了解一下 Go 中是如何解析 URL 的。

package main

Expand All @@ -9,46 +9,42 @@ import "strings"

func main() {

// We'll parse this example URL, which includes a
// scheme, authentication info, host, port, path,
// query params, and query fragment.
// 我们将解析这个 URL 示例,它包含了一个 scheme,
// 认证信息,主机名,端口,路径,查询参数和片段。
s := "postgres://user:[email protected]:5432/path?k=v#f"

// Parse the URL and ensure there are no errors.
// 解析这个 URL 并确保解析没有出错。
u, err := url.Parse(s)
if err != nil {
panic(err)
}

// Accessing the scheme is straightforward.
// 直接访问 scheme
fmt.Println(u.Scheme)

// `User` contains all authentication info; call
// `Username` and `Password` on this for individual
// values.
fmt.Println(u.User)
// `User` 包含了所有的认证信息,这里调用 `Username`
// 和 `Password` 来获取独立值。
fmt.Println(u.User)
fmt.Println(u.User.Username())
p, _ := u.User.Password()
fmt.Println(p)

// The `Host` contains both the hostname and the port,
// if present. `Split` the `Host` manually to extract
// the port.
fmt.Println(u.Host)
// `Host` 同时包括主机名和端口信息,如过端口存在的话,
// 使用 `strings.Split()` 从 `Host` 中手动提取端口。
fmt.Println(u.Host)
h := strings.Split(u.Host, ":")
fmt.Println(h[0])
fmt.Println(h[1])

// Here we extract the `path` and the fragment after
// the `#`.
// 这里我们提出路径和查询片段信息。
fmt.Println(u.Path)
fmt.Println(u.Fragment)

// To get query params in a string of `k=v` format,
// use `RawQuery`. You can also parse query params
// into a map. The parsed query param maps are from
// strings to slices of strings, so index into `[0]`
// if you only want the first value.
// 要得到字符串中的 `k=v` 这种格式的查询参数,可以使
// `RawQuery` 函数。你也可以将查询参数解析为一个
// map。已解析的查询参数 map 以查询字符串为键,对应
// 值字符串切片为值,所以如何只想得到一个键对应的第
// 一个值,将索引位置设置为 `[0]` 就行了。
fmt.Println(u.RawQuery)
m, _ := url.ParseQuery(u.RawQuery)
fmt.Println(m)
Expand Down
4 changes: 2 additions & 2 deletions examples/url-parsing/url-parsing.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Running our URL parsing program shows all the different
# pieces that we extracted.
# 运行我们的 URL 解析程序,显示全部我们提取的 URL 的
# 不同数据块。
$ go run url-parsing.go
postgres
user:pass
Expand Down

0 comments on commit 92541e2

Please sign in to comment.