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

huangapple go评论89阅读模式
英文:

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

问题

我正在尝试按关键字搜索YouTube视频,就像golang示例中的代码一样。我稍微修改了那段代码,让它可以多次使用不同的关键字进行搜索。

当我只搜索一次时,一切正常。

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开发者密钥"

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)
}

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

我无法在运行时更改标志,但如何在一个程序中使用两个不同的关键字进行搜索?

更新

有效的解决方案:

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密钥"

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)
}

现在你可以在一个程序中使用两个不同的关键字进行搜索了。

英文:

I am trying to search YouTube video by key like in the golang example. I modified that code a little to let it search by different keys several times.

When I search once it is ok.

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)
}

But if I search twice ...

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)
}

... then it panics with error message ...

flag redefined: query

... on line ...

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

Full code:

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)
}

So it is impossible to use this code on a website. Only the first user will be able to search first time, the others will fail.

I cannot change flag during runtime but how to search by 2 different keys in one program?

Update

working solution:

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)
}

答案1

得分: 2

恐慌消息会告诉你出了什么问题。你的命令行标志应该只定义一次。如果你在运行时尝试重新定义它们,就会引发恐慌。

英文:

The panic message tells you exactly what's wrong. Your command line flags should be defined only once. If you try to redefine them during runtime, it panics.

huangapple
  • 本文由 发表于 2015年4月14日 22:52:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/29630598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定