如何在Go SDK中使用Couchbase N1QL查询扩展

In the golang Couchbase SDK there are N1QL select examples documented with examples like this:

myQuery := gocb.NewN1qlQuery("SELECT airportname, city, country FROM `travel-sample` " +
        "WHERE type='airport' AND city=$1 ")
myParams = append(myParams, []interface{}{"Reno"})
rows, err := bucket.ExecuteN1qlQuery(myQuery, myParams)

When I try it with the following, it doesn't find any records.

query := gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE 'TD:$1:%'")

rows, err := r.Bucket.ExecuteN1qlQuery(query, []interface{}{userid})
if err != nil {
    return nil, err
}

But this does work and also works in the Couchbase console:

query := gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE 'TD:"+userid+":%'")
rows, err := bucket.ExecuteN1qlQuery(query, nil)

Am I doing anything obviously wrong here?

While the non-parametised option works I'd like to use the parametised one and mark it as a prepared statement with query.AdHoc(false)

gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE 'TD:$1:%'")

In above code your query parameter is include inside the quotes. query parameters inside string will not be replaced. So it is looking for document id "TD:$1:"

You should try this

gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE 'TD:' || $1 || ':%' ")

OR

   gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE $1")
    Pass $1  'TD:'+userid+':%' 

OR

gocb.NewN1qlQuery("SELECT * FROM `bucket` WHERE META().id LIKE 'TD:"+userid+":%'")