Using Separator in Strings.join GoLang

I am creating a front url url

var SearchUrl = "https://www.example.org/3/search/movie?query="

Then create data that contains the keywords to search.

var MovieSearch []string = r.Form["GetSearchKey"]  

And the third part of the url that contains the api key

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

I use ArrayToString()to analyze the input of the form

func ArrayToString(array []string) string{
    str := strings.Join(array, "+")
    return str 
}

And then by building such a URL,

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 result of a single-word URL works fine. The url is as follows:

https://api.example.org/3/search/movie?query=+bad+&api_key=#####

When I add the second word in the input field, the url looks like this:

https://api.example.org/3/search/movie?query=bad santa&api_key=e######

I need keywords so that there is no space between them, as in a URL that does not work.

This is my current attempt based on a response 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
-6
source share
4 answers

Usually, you need to use the URL package values.

, , , , , http.HandlerFunc :

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
}

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

, , .

+2

, https://golang.org/pkg/net/url/#QueryEscape, , .

, https://golang.org/pkg/net/url/#URL URL:

params := fmt.Sprintf("?query=%s&api_key=######", url.QueryEscape("name"))
perform := url.URL{
    Scheme:     "https",  
    Host:       "api.example.com",
    Path:       "3/search/movie",
    RawQuery:   params,
}

fmt.Println(perform) // <- Calls .String()

https://golang.org/doc/effective_go.html.

[]:

func ArrayToQuery(values []string) string {
    return url.QueryEscape(strings.Join(values, " "))
}
+1

MovieSearch "bad santa", , , . "+" .

0
source

If a word has a space, you need to replace it.

Example

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Replace("bad santa", " ", "+", -1))
}

So you should probably do it like this

func main() {
    a := []string{"bad", "santa"}
    fmt.Printf("%q\n", a)
    j := ArrayToString(a)
    strings.Replace(j, " ", "+",-1)
    fmt.Printf("%q\n", j)
}

Here is a link to the Go documentation - https://golang.org/pkg/strings/#Replace

0
source

All Articles