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

在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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?