drb88830 2016-03-14 02:26
浏览 325
已采纳

在Strings.join GoLang中使用分隔符

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"]  

And the third part of the url which holds the api key

var apiKey = "&api_key=######"

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)

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=#####

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.

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()
}

Error

./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
  • 写回答

4条回答 默认 最新

  • dsuxcxqep31023992 2016-03-15 00:19
    关注

    Normally, one should use url package's Values.

    Here's an example, that does what I think you want, on play Both 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
    }
    

    Output: Perform: https://www.example.org/3/search/movie?query=this+is+a+value

    Notice how the values are put in to query string, properly escaped, for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 openwrt双栈NAT
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用