标志重新定义 - 在 YouTube 数据中按键恐慌搜索 API 3

flag redefined - panic searching by key in YouTube data API 3

我正在尝试像 the golang example 中那样按键搜索 YouTube 视频。我稍微修改了那个代码,让它通过不同的键搜索几次。

当我搜索一次就可以了。

func main() {
    result1, err1 := SearchYoutubeByKey("hello")

    if err1 != nil {
        panic(err1)
    }
    fmt.Println(result1)

    // result2, err2 := SearchYoutubeByKey("world")
    // if err2 != nil {
    //  panic(err2)
    // }
    // fmt.Println(result2)
}

但是如果我搜索两次...

func main() {
    result1, err1 := SearchYoutubeByKey("hello")

    if err1 != nil {
        panic(err1)
    }
    fmt.Println(result1)

    result2, err2 := SearchYoutubeByKey("world")
    if err2 != nil {
        panic(err2)
    }
    fmt.Println(result2)
}

...然后它出现错误消息...

flag redefined: query

...在线...

query := flag.String("query", str, "Search term")

完整代码:

package main

import (
    "code.google.com/p/google-api-go-client/googleapi/transport"
    "code.google.com/p/google-api-go-client/youtube/v3"
    "flag"
    "fmt"
    "net/http"
)

var (
    maxResults = flag.Int64("max-results", 25, "Max YouTube results")
    service    *youtube.Service
    response   *youtube.SearchListResponse
)

const developerKey = "youtube developer key"

type YoutubeSearchResult struct {
    Title, YoutubeId string
}

func SearchYoutubeByKey(str string) (result []*YoutubeSearchResult, err error) {
    query := flag.String("query", str, "Search term")
    flag.Parse()

    client := &http.Client{
        Transport: &transport.APIKey{Key: developerKey},
    }

    service, err = youtube.New(client)
    if err != nil {
        return
    }

    // Make the API call to YouTube.
    call := service.Search.List("id,snippet").
        Q(*query).
        MaxResults(*maxResults)
    response, err = call.Do()
    if err != nil {
        return
    }

    // Iterate through each item and add it to the correct list.
    for _, item := range response.Items {
        switch item.Id.Kind {
        case "youtube#video":
            result = append(result, &YoutubeSearchResult{Title: item.Snippet.Title, YoutubeId: item.Id.VideoId})
        }
    }
    return
}

func main() {


    result1, err1 := SearchYoutubeByKey("hello")

    if err1 != nil {
        panic(err1)
    }
    fmt.Println(result1)

    result2, err2 := SearchYoutubeByKey("world")
    if err2 != nil {
        panic(err2)
    }
    fmt.Println(result2)
}

因此不可能在网站上使用此代码。只有第一个用户可以第一次搜索,其他用户将失败。

我无法在运行时更改标志,但如何在一个程序中通过 2 个不同的键进行搜索?

更新

工作解决方案:

package main

import (
    "code.google.com/p/google-api-go-client/googleapi/transport"
    "code.google.com/p/google-api-go-client/youtube/v3"
    "flag"
    "fmt"
    "net/http"
)

var (
    maxResults = flag.Int64("max-results", 25, "Max YouTube results")
    service    *youtube.Service
    response   *youtube.SearchListResponse
    query      = flag.String("query", "str", "Search term")
)

const developerKey = "youtube api key"

type YoutubeSearchResult struct {
    Title, YoutubeId string
}

func SearchYoutubeByKey(str string) (result []*YoutubeSearchResult, err error) {
    flag.Parse()

    client := &http.Client{
        Transport: &transport.APIKey{Key: developerKey},
    }

    service, err = youtube.New(client)
    if err != nil {
        return
    }

    // Make the API call to YouTube.
    call := service.Search.List("id,snippet").
        Q(str).
        MaxResults(*maxResults)
    response, err = call.Do()
    if err != nil {
        return
    }

    // Iterate through each item and add it to the correct list.
    for _, item := range response.Items {
        switch item.Id.Kind {
        case "youtube#video":
            result = append(result, &YoutubeSearchResult{Title: item.Snippet.Title, YoutubeId: item.Id.VideoId})
        }
    }
    return
}

func main() {
    result1, err1 := SearchYoutubeByKey("hello")

    if err1 != nil {
        panic(err1)
    }
    fmt.Println(result1)

    result2, err2 := SearchYoutubeByKey("world")
    if err2 != nil {
        panic(err2)
    }
    fmt.Println(result2)
}

紧急消息会告诉您到底出了什么问题。您的命令行标志应该只定义一次。如果您尝试在运行时重新定义它们,它会崩溃。