Home > Article > Backend Development > Discuss the causes and solutions of golang oracle garbled characters
When Golang uses the Oracle database, it is common to have garbled characters. The specific manifestation is that Chinese characters or other special characters inserted in the database cannot be displayed correctly. This problem may have multiple causes, let’s analyze and solve them one by one below.
First of all, we need to confirm whether the Oracle database itself supports the Chinese character set. During the creation process of the database, you need to choose to support Chinese character sets, such as UTF8 encoding, so that Chinese characters can be correctly saved and displayed. If the corresponding character set is not selected when creating the database, garbled characters may easily occur during use.
Secondly, we need to check whether the character set is correctly set in the Golang program. In Golang, we can handle character sets by setting the encoding of source code files or using specific libraries. If the character set used by the program is inconsistent with the database, garbled characters will occur. In Golang code, you can use the following code to set the character set:
db, err := sql.Open("ora", "user/pass@192.168.1.100:1521/ORCL") if err != nil { log.Fatal(err) } db.SetConnMaxLifetime(time.Hour) db.SetMaxIdleConns(10) db.SetMaxOpenConns(100) db.Exec("ALTER SESSION SET NLS_LANGUAGE='SIMPLIFIED CHINESE'") db.Exec("ALTER SESSION SET NLS_TERRITORY='CHINA'") db.Exec("ALTER SESSION SET NLS_CHARACTERSET='AL32UTF8'")
In the above example, the database/sql
package of Go language is used to connect to the Oracle database and execute Oracle's SQL script to set character set. Among them, NLS_CHARACTERSET
is set to UTF8 encoding. This character set can save Chinese characters and other international characters.
Finally, we need to check that the data is encoded and decoded correctly. If the data is correctly encoded when written to the database (saved using UTF8 encoding), it will also need to be decoded using UTF8 to display correctly when read. We can use Golang's encoding
package to perform encoding and decoding operations, for example:
src := "你好,世界" dst1 := make([]byte, len(src)*3) dst1 = []byte(src) dst2, err := unicode.UTF32(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder().Bytes([]byte(src)) if err != nil { log.Fatal(err) } println(string(dst1)) println(string(dst2))
In the above example, the encoding/unicode
package of Go language is used for encoding. , dst1
saves Chinese characters as they are, and dst2
is UTF32 encoded, so that the UTF32 character set can be used in Oracle to save and read data.
To sum up, to solve the problem of garbled characters when Golang uses Oracle, we need to ensure that the database itself supports the Chinese character set, ensure that the Golang program correctly sets the character set, and implements correct data encoding and decoding. Only if these steps are performed correctly can the problem of garbled characters be finally solved.
The above is the detailed content of Discuss the causes and solutions of golang oracle garbled characters. For more information, please follow other related articles on the PHP Chinese website!