err := ctx.ShouldBindJSON(&_in)
before pass this function we want to remove the escape string from the json "VALUE"
data, err := services.MyServicefunction(_in)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"exception": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"exception": "", "data": data})
You can accomplish this in an arguably easier and more performant (though performance is almost certainly not an issue and I would hate to be accused of premature optimization) way using a string replacer:
package main
import (
"fmt"
"strings"
)
func main() {
testStr := "jhon\r\t\n"
// Replacer to replace all unwanted characters
r := strings.NewReplacer("\n", "", "\r", "", "\t", "")
fmt.Println(r.Replace(testStr))
}
Here’s a playground link. If you find having your strings on a single line harder to read than your strings.ReplaceAll implementation, you can separate them out like so:
r := strings.NewReplacer(
"\n", "",
"\r", "",
"\t", "",
)
This also makes it obvious if you accidentally create an odd number of strings, in which case you will get an error.