问题描述
我正在建立一个URL前面的URL
I am building a URL with the front of the url
var SearchUrl = "https://www.example.org/3/search/movie?query="
然后形成保存要搜索关键字的数据。
Then form data which holds the keywords to search for.
var MovieSearch []string = r.Form["GetSearchKey"]
以及保存api键的url的第三部分
And the third part of the url which holds the api key
var apiKey = "&api_key=######"
我是使用 ArrayToString()
来解析表单输入数据
I am using the ArrayToString()
to parse the form input data
func ArrayToString(array []string) string{
str := strings.Join(array, "+")
return str
}
然后构建这样的网址,
And then building the url like this,
var SearchUrl = "https://api.example.org/3/search/movie?query="
var MovieSearch []string = r.Form["GetSearchKey"]
var apiKey = "&api_key=########"
UrlBuild := []string {SearchUrl, ArrayToString(MovieSearch), apiKey}
OUTPUT_STRING := ArrayToString(UrlBuild)
一个词的URL输出完美。网址如下所示:
The output of the URL with one word works perfectly. The URL looks like this,
https://api.example.org/3/search/movie?query=+bad+&api_key=#####
当我在输入字段中添加第二个字时, url看起来像这样,
When I add a second word to the input field the url looks like this,
https://api.example.org/3/search/movie?query=bad santa&api_key=e######
我需要关键字在它们之间没有空格,就像该网址无效。
I need the keywords to not have a space between them like in the url that does not work.
这是我当前基于Cedmundo的答案进行的尝试
This is my current attempt based on the answer from Cedmundo
func ArrayToQuery(values []string) string {
return url.QueryEscape(strings.Join(values, " "))
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
display(w, "search", &Page{Title: "Search"})
fmt.Println("method:", r.Method)
r.ParseForm()
var MovieSearch = r.Form["GetSearchKey"]
var SearchKeys = ArrayToQuery(MovieSearch)
params := fmt.Sprintf("?query=%s&api_key=eagaggagagagagagag", url.QueryEscape(SearchKeys))
perform := url.URL{
Scheme: "https",
Host: "api.example.org",
Path: "3/search/movie",
RawQuery: params,
}
fmt.Println(perform) // <- Calls .String()
}
错误
./main.go:63: url.QueryEscape undefined (type string has no field or method QueryEscape)
./main.go:75: url.QueryEscape undefined (type string has no field or method QueryEscape)
./main.go:76: url.URL undefined (type string has no field or method URL)
./main.go:94: undefined: OUTPUT_STRING
./main.go:96: undefined: OUTPUT_STRING
推荐答案
正常情况下,应该使用url包的值。
Normally, one should use url package's Values.
下面是一个例子,这是我认为你想要的, a href =http://play.golang.org/p/9gC6NNW--_ =nofollow>在玩
无论是简单的main还是http.HandlerFunc形式:
Here's an example, that does what I think you want, on playBoth a simple main, and in http.HandlerFunc form:
package main
import "fmt"
import "net/url"
import "net/http"
func main() {
baseURL := "https://www.example.org/3/search/movie"
v := url.Values{}
v.Set("query", "this is a value")
perform := baseURL + "?" + v.Encode()
fmt.Println("Perform:", perform)
}
func formHandler(w http.ResponseWriter, r *http.Request) {
baseURL := "https://www.example.org/3/search/movie"
v := url.Values{}
v.Set("query", r.Form.Get("GetSearchKey")) // take GetSearchKey from submitted form
v.Set("api_ley", "YOURKEY") // whatever your api key is
perform := baseURL + "?" + v.Encode() // put it all together
fmt.Println("Perform:", perform) // do something with it
}
输出:
执行:https://www.example.org/3/search/movie?query = this +是+ a +值
请注意,这些值是否会放入查询字符串中,以便您正确转义。
Notice how the values are put in to query string, properly escaped, for you.
这篇关于在Strings.join GoLang中使用Separator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!