Home > Blockchain >  I would like to add Symbol to Query Format. How should I add it?
I would like to add Symbol to Query Format. How should I add it?

Time:11-01

func (s *TODOService) DeleteTODO(ctx context.Context, ids []int64) error {
    const deleteFmt = `DELETE FROM todos WHERE id IN (?%s)`
    
    return nil
}

I need to add as many Symbols (here ?) to the Query as the number of id lists, so I want to combine the fmt.Sprintf and strings.Repeat functions to add Symbols to the provided Query Format How should I add it?

CodePudding user response:

ipml Stringer interface

package main

import (
    "fmt"
    "strconv"
    "strings"
)

type SqlArr []int64

func (a SqlArr) String() string {
    ans := strings.Builder{}
    for i := 0; i < len(a); i   {
        ans.WriteString(strconv.FormatInt(a[i], 10))
        if i != len(a)-1 {
            ans.WriteRune(',')
        }
    }
    return ans.String()
}

func Test(ids SqlArr) {
    deleteFmt := fmt.Sprintf(`DELETE FROM todos WHERE id IN (%s)`, ids)
    fmt.Printf("%v\n", deleteFmt)
}

func main() {
    Test([]int64{1, 2, 3})
}

  •  Tags:  
  • go
  • Related