将有效的4字节UTF-8序列插入MySQL时出错

I have the following UTF-8 byte string I'm trying to store in a MySQL table (utf8mb4) but getting a failure from the mysql server.

...
db.Exec("SET NAMES 'utf8mb4'; SET CHARACTER SET utf8mb4;")

var badBytes = []byte{
    34, 48, 34, 32, 47, 62, 66, 117, 121, 32, 105, 116, 32, 110, 111, 119, 32,
    240, 159, 147, 149, 32, 60, 97, 32, 104, 114, 101, 102, 61, 34, 104, 116,
}

fmt.Println("UTF8 Valid", utf8.Valid(badBytes))
fmt.Println()
fmt.Println(string(badBytes))
fmt.Println()

res, err := db.Exec("INSERT INTO demo (body) VALUES (?)", string(badBytes))
if err != nil {
    log.Fatal(err)
}

id, err := res.LastInsertId()
fmt.Println(id, err)

The output is below

UTF8 Valid true

="1" border="0" />Buy it now

Like @Rico said I needed to specify the collation (utf8mb4_unicode_ci) so the go-mysql lib didn't default to utf8 and cause problems with my 4byte unicode runes (mysql's utf8 only supports up to 3byte unicode).

[username[:password]@][protocol[(address)]]/dbname?collation=utf8mb4_unicode_ci

See the go-sql-driver readme for more information.