我想将符号添加到查询格式。我应该如何添加它?

func (s *TODOService) DeleteTODO(ctx context.Context, ids []int64) error {

    const deleteFmt = `DELETE FROM todos WHERE id IN (?%s)`

    

    return nil

}

我需要?向查询中添加与 id 列表数量一样多的符号(此处),因此我想结合 fmt.Sprintf 和 strings.Repeat 函数以将符号添加到提供的查询格式中 我应该如何添加它?


白猪掌柜的
浏览 67回答 1
1回答

白板的微信

ipmlStringer interfacepackage mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "strings")type SqlArr []int64func (a SqlArr) String() string {&nbsp; &nbsp; ans := strings.Builder{}&nbsp; &nbsp; for i := 0; i < len(a); i++ {&nbsp; &nbsp; &nbsp; &nbsp; ans.WriteString(strconv.FormatInt(a[i], 10))&nbsp; &nbsp; &nbsp; &nbsp; if i != len(a)-1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ans.WriteRune(',')&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return ans.String()}func Test(ids SqlArr) {&nbsp; &nbsp; deleteFmt := fmt.Sprintf(`DELETE FROM todos WHERE id IN (%s)`, ids)&nbsp; &nbsp; fmt.Printf("%v\n", deleteFmt)}func main() {&nbsp; &nbsp; Test([]int64{1, 2, 3})}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go