您可以使用使用中等點(組成一個非ASCII字符,並用urlencode,例如+^+在linux上):
const sep = `·`
const t = `http = https://stackoverflow.com/questions·string to search for·header=value·header=value`
func parseLine(line string) (name, url, search string, headers []string) {
idx := strings.Index(line, " = ")
if idx == -1 {
return
}
name = line[:idx]
parts := strings.Split(line[idx+3:], sep)
if len(parts) < 3 {
// handle invalid line
}
url, search = parts[0], parts[1]
headers = parts[2:]
return
}
儘管使用JSON可能是最好的和最長期的可維護選項。
爲了完整起見,JSON版本會是什麼樣子:
type Site struct {
Url string
Query string
Headers map[string]string
}
const t = `[
{
"url": "https://stackoverflow.com/questions",
"query": "string to search for",
"headers": {"header": "value", "header2": "value"}
},
{
"url": "https://google.com",
"query": "string to search for",
"headers": {"header": "value", "header2": "value"}
}
]`
func main() {
var sites []Site
err := json.Unmarshal([]byte(t), &sites)
fmt.Printf("%+v (%v)\n", sites, err)
}
如果你有控制格式,爲什麼不使用json或csv? – OneOfOne 2014-10-05 18:59:33
@OneOfOne:我開始在CSV中實現它。所以我讀了=使用Go的標準csv閱讀器的右側。開始,除非我陷入困境。然後我的想法是使標題最後作爲vargs ... – 2014-10-05 19:07:59