Go 1.10编译报错无效UTF-8编码,但Go Playground可运行求因
Hey there! Let's dig into this UTF-8 error you're hitting with your Go example code. The compiler already gave you a super specific clue—line 32, column 15—so that's our starting point. Here's how to track down and fix the issue:
Step 1: Inspect the Exact Location
First, open Example#2.go and jump straight to line 32. Count carefully to the 15th character (include spaces, punctuation, and any string characters). Common culprits here are:
- Invisible control characters: Things like zero-width spaces, soft hyphens, or full-width whitespace that look like regular spaces but aren't valid UTF-8.
- Corrupted non-ASCII characters: If you're using non-English text (for comments or string literals), a single character might have been mangled during copy-paste or manual input.
- Invalid string escapes: If line 32 has a string literal, check for malformed escape sequences (e.g.,
\xfollowed by an invalid hex value, or unclosed quotes that sneak in invalid bytes).
Step 2: Use Tools to Uncover Hidden Issues
If you can't spot the problem with a quick glance, use these tools to get a clearer view:
- Show invisible characters in your editor: Most modern editors (VS Code, Notepad++, Sublime Text) have a setting to render control characters. For VS Code, enable "Render Control Characters" via the command palette (
Ctrl+Shift+P→ "Toggle Render Control Characters"). - Check the raw byte sequence: On Windows PowerShell, run this command to see the exact bytes of line 32 (note: PowerShell uses zero-indexing, so line 32 is index 31):
Look for byte sequences that don't follow valid UTF-8 rules (e.g., a single byte starting with(Get-Content .\Example#2.go)[31] | Format-Hex10xxxxxx, which is a continuation byte without a leading byte). - Validate the entire file: If you have Git Bash or WSL, use the
filecommand to check the file's encoding:
It should show "UTF-8 Unicode text"—if it says something else (like "ISO-8859 text"), that's a red flag.file Example#2.go
Step 3: Fix the Issue
Once you've identified the problematic character:
- Replace any invalid control characters with their ASCII equivalents (e.g., full-width space → regular space).
- Re-type any corrupted non-ASCII characters manually (avoid copying from sources that might have encoding issues).
- Fix any malformed string escapes—make sure all escape sequences follow Go's rules (e.g.,
\uXXXXfor Unicode code points, valid hex values for\x).
Quick Example Scenario
Suppose line 32 looks like this:
fmt.Println("Welcome to Go 世界!")
If the character "世" was corrupted during copy-paste, re-typing it manually should resolve the error. Alternatively, if there's a zero-width space after "Go", deleting that invisible character will fix the invalid UTF-8.
Since this is a teaching example, sticking to ASCII characters where possible can help avoid these issues entirely, but if you need non-ASCII text, just double-check that your editor saves the file as valid UTF-8.
内容的提问来源于stack exchange,提问作者alm2022

