duanshan1511
2016-03-13 23:33
浏览 212
已采纳

在GoLang中的For循环中将值转换为字符串

In GoLang I am looping through my form data like this,

for key, values := range r.Form {   // range over map
    for _, value := range values {    // range over []string
        fmt.Println(key, value)
    }
}

I can print the data out to the terminal like this but I need this data to be a string so I can access it from outside of the for loop.

How can I Put this in a variable that I can call outside of the for loop when the loop is finished?

My goal here is to build a url by joining these three strings

var SearchUrl = "https://api.themoviedb.org/3/search/movie?query="
var MovieSearch []string = r.Form["GetSearchKey"]   
var apiKey = "&api_key=e2a"
UrlBuild := []string {SearchUrl, MovieSearch, apiKey}
fmt.Println(UrlBuild) 

I get this error

/main.go:71: cannot use MovieSearch (type []string) 
as type string in array or slice literal

MovieSearch is coming from the input form. It is the search keywords.

Once I have one string I can pass this to a function that makes the api call.

The full function

func searchHandler(w http.ResponseWriter, r *http.Request) {
  display(w, "search", &Page{Title: "Search"})
   fmt.Println("method:", r.Method) 
        r.ParseForm()
        fmt.Println("GetSearchKey:", r.Form["GetSearchKey"])

for key, values := range r.Form {   // range over map
for _, value := range values {    // range over []string
    fmt.Println(key, value)

  }
}

var SearchUrl = "https://api.themoviedb.org/3/search/movie?query="
 var MovieSearch []string = r.Form["GetSearchKey"]   
var apiKey = "&api_key=ewrfwrfwrcwerc"
UrlBuild := []string {SearchUrl, MovieSearch, apiKey}
fmt.Println(UrlBuild)

fmt.Println(reflect.TypeOf(r.Form["GetSearchKey"] ))
}

The error comes from this line,

UrlBuild := []string {SearchUrl, MovieSearch, apiKey}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duan0708676887 2016-03-14 00:50
    已采纳

    You cannot have a slice within a slice.

    func ArrayToString(array []string) string {
        str := strings.Join(array, "")
        return str
    
    }
    UrlBuild := []string {SearchUrl, ArrayToString(MovieSearch), apiKey}
    fmt.Println(UrlBuild) 
    UrlBuildString := ArrayToString(UrlBuild)
    

    OR

    UrlBuildString := ArrayToString([]string{SearchUrl, ArrayToString(MovieSearch), apiKey}
    

    _________________FULL_______________

    func ArrayToString(array []string) string {
        str := strings.Join(array, "")
        return str
    
    }
    func searchHandler(w http.ResponseWriter, r *http.Request) {
      display(w, "search", &Page{Title: "Search"})
       fmt.Println("method:", r.Method) 
            r.ParseForm()
            fmt.Println("GetSearchKey:", r.Form["GetSearchKey"])
    
    for key, values := range r.Form {   // range over map
    for _, value := range values {    // range over []string
        fmt.Println(key, value)
    
      }
    }
    
    var SearchUrl = "https://api.themoviedb.org/3/search/movie?query="
     var MovieSearch []string = r.Form["GetSearchKey"]   
    var apiKey = "&api_key=ewrfwrfwrcwerc"
    UrlBuild := []string {SearchUrl, ArrayToString(MovieSearch), apiKey}
    fmt.Println(UrlBuild)
    OUTPUT_STRING := ArrayToString(UrlBuild)
    }
    
    打赏 评论
  • doujian3132 2016-03-14 00:05

    This problem doesn't seem so bad, though I may be misunderstanding the question. There are many ways to build up these strings.

    Consider the following approach using concatenation (slow):

    s := []string{}
    for key, values := range r.Form {   // range over map
        for _, value := range values {    // range over []string
            s = append(s, fmt.Sprintf("%s, %s", key, value))
        }
    }
    data := strings.Join(s, ";")
    

    More examples of concat are seen here: http://herman.asia/efficient-string-concatenation-in-go

    打赏 评论

相关推荐 更多相似问题