Decoding a string from the non-Unicode charset
A lesser-known fact is that all content in .go
files is encoded in UTF-8. Believe it or not the Unicode is not, the only charset in the world. For example, the Windows-1250 encoding is widely spread across Windows users.
When working with non-Unicode strings, you need to transcode the content to Unicode. This recipe demonstrates how to decode and encode the non-Unicode strings.
How to do it...
- Open the console and create the folder
chapter02/recipe08
. - Navigate to the directory.
- Create the file
win1250.txt
with contentGdańsk
. The file must be encoded in the windows-1250 charset. If you are not sure how to do that, just jump to step 6 and after you complete step 7, which will create the windows-1250 encoded file, you can rename theout.txt
file and go back to step 4. - Create the
decode.go
file with the following content:
package main import ( "fmt" "io/ioutil" "os" "strings" "golang...