Answer the question
In order to leave comments, you need to log in
How to write in MySql Unicode in Golang?
I have a data structure in go.
I am doing json.Marshal(data) and then trying to write this line to MySql.
I do it like this:
data_json, err := json.Marshal(data)
db, err := sql.Open("mysql", ...)
stmt, err := db.Prepare("INSERT INTO my_table (data) VALUES (?)")
res, err = stmt.Exec(data_json)
Error 1366: Incorrect string value: '\xF0\x9F\x8E\x81 \xD0...' for column 'data' at row 1
Answer the question
In order to leave comments, you need to log in
The problem is emoji. Some of them are encoded with 4 bits.
Found 2 solutions.
1st:
2nd:
func RemoveEmoji(str string) string {
var emojiiPattern = "[\\x{1f004}\\x{1f0cf}\\x{1f170}\\x{1f171}\\x{1f17e}\\x{1f17f}\\x{1f18e}\\x{1f191}-\\x{1f19a}\\x{1f201}\\x{1f202}\\x{1f21a}\\x{1f22f}\\x{1f232}-\\x{1f23a}\\x{1f250}\\x{1f251}\\x{1f300}-\\x{1f321}\\x{1f324}-\\x{1f393}\\x{1f396}\\x{1f397}\\x{1f399}-\\x{1f39b}\\x{1f39e}-\\x{1f3f0}\\x{1f3f3}-\\x{1f3f5}\\x{1f3f7}-\\x{1f4fd}\\x{1f4ff}-\\x{1f53d}\\x{1f549}-\\x{1f54e}\\x{1f550}-\\x{1f567}\\x{1f56f}\\x{1f570}\\x{1f573}-\\x{1f579}\\x{1f587}\\x{1f58a}-\\x{1f58d}\\x{1f590}\\x{1f595}\\x{1f596}\\x{1f5a5}\\x{1f5a8}\\x{1f5b1}\\x{1f5b2}\\x{1f5bc}\\x{1f5c2}-\\x{1f5c4}\\x{1f5d1}-\\x{1f5d3}\\x{1f5dc}-\\x{1f5de}\\x{1f5e1}\\x{1f5e3}\\x{1f5ef}\\x{1f5f3}\\x{1f5fa}-\\x{1f64f}\\x{1f680}-\\x{1f6c5}\\x{1f6cb}-\\x{1f6d0}\\x{1f6e0}-\\x{1f6e5}\\x{1f6e9}\\x{1f6eb}\\x{1f6ec}\\x{1f6f0}\\x{1f6f3}\\x{1f910}-\\x{1f918}\\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\\x{1f1e6}\\x{1f1f2}\\x{1f1fc}]";
var emojiRx = regexp.MustCompile(emojiiPattern)
return emojiRx.ReplaceAllString(str, `[emoji]`)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question