This question already has an answer here:
Suppose there are 1000 user record in the csv file while reading that file 700 user have in there Last name this sign ’
and I have to change that sign to '
using golang. Is there any technique or function for this Which solve my problem?
One thing is that while I print this ’
for example
package main
import (
"fmt"
)
func main() {
strVal := "O’max"
for i:= 0; i< len(strVal);i++{
fmt.Println(string(strVal[i]))
}
}
Output is :-
O
â
m
a
x
</div>
You're challenging with unicode issue. The problem is that you iterate over bytes, but not over characters. At the same time, your unicode character ’
takes 3 bytes, that's why you have that output. In order to handle it correctly, you have iterate with rune, which will handle unicode bytes.
func convert(src string) string {
var sb strings.Builder
for _, el := range src {
if el == '’' {
sb.WriteString("'")
} else {
sb.WriteRune(el)
}
}
return sb.String()
}
And then, where you need to convert, you have to call
strVal = convert(strVal)
Your full scenario with is in Playground
Read more about rune in Golang Blog