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

在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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度