package main
import (
"fmt"
"strings"
)
const selectBase = "SELECT * FROM user WHERE %s "
var refStringSlice = []string{
" FIRST_NAME = 'Jack' ",
" INSURANCE_NO = 333444555 ",
" EFFECTIVE_FROM = SYSDATE "}
func main() {
sentence := strings.Join(refStringSlice, "AND")
fmt.Printf(selectBase+"
", sentence)
}
/*
SELECT * FROM user WHERE FIRST_NAME = 'Jack' AND INSURANCE_NO = 333444555 AND EFFECTIVE_FROM = SYSDATE
*/
package main
import (
"fmt"
"strings"
)
const selectBase = "SELECT * FROM user WHERE "
var refStringSlice = []string{
" FIRST_NAME = 'Jack' ",
" INSURANCE_NO = 333444555 ",
" EFFECTIVE_FROM = SYSDATE "}
type JoinFunc func(piece string) string
func main() {
jF := func(p string) string {
if strings.Contains(p, "INSURANCE") {
return "OR"
}
return "AND"
}
result := JoinWithFunc(refStringSlice, jF)
fmt.Println(selectBase + result)
}
func JoinWithFunc(refStringSlice []string, joinFunc JoinFunc) string {
concatenate := refStringSlice[0]
for _, val := range refStringSlice[1:] {
concatenate = concatenate + joinFunc(val) + val
}
return concatenate
}
/*
SELECT * FROM user WHERE FIRST_NAME = 'Jack' OR INSURANCE_NO = 333444555 AND EFFECTIVE_FROM = SYSDATE
*/