Home>

I want to run the youtube API on Go and get a list of videos.
But for some reason cannot use "id, snippet" (type string) as type [] string
Is displayed and it seems that I can not hit the API, but I do not understand why I will ask you a question.
In the first place, is it not possible to authenticate with the API Key or is the Call method wrong?
(I think it's probably the former, but I'd appreciate it if you could give us your opinion.)

package api
import (
    "context"
    "github.com/labstack/echo"
    "github.com/sirupsen/logrus"
    "github.com/valyala/fasthttp"
    "google.golang.org/api/option"
    "google.golang.org/api/youtube/v3"
)
func FetchMostPopularVideos () echo.HandlerFunc {
    return func (c echo.Context) error {
        key: = "My API_Key"
        ctx: = context.Background ()
        yts, err: = youtube.NewService (ctx, option.WithAPIKey (key))
        if err! = nil {
            logrus.Fatalf ("Error creating new Youtube service:% v", err)
        }
        call: = yts.Videos.List ("id, snippet"). Chart ("mostPopular"). MaxResults (3)
        res, err: = call.Do ()
        if err! = nil {
            logrus.Fatalf ("Error calling Youtube API:% v", err)
        }
        return c.JSON (fasthttp.StatusOK, "Most Popular")
    }
}
  • Answer # 1

    If possible, if you write the error line display as it is, it will be easier to answer by narrowing down the points to be noted.

    cannot use "id, snippet" (type string) as type [] string


    You probably get this error

    call: = yts.Videos.List ("id, snippet"). Chart ("mostPopular"). MaxResults (3)


    I think this line.

    The definition of this List method is

    func (r * VideosService) List (part [] string) * VideosListCall


    It looks like this.
    The argument of the List method is[] stringOnly the type is accepted. there"id, snippet"The problem is that I'm trying to pass a single string.