Go development practice: solve the problem of module dependency that the IDE reports an error but the program runs normally
The background of the problem
- Implement Reader exercises when learning Go Tour
- The code logic is correct, and the command line runs successfully output ‘OK!’
- But vscode IDE continues to display the import error message
2. The first pit: depend on download failure (502 bad gateway), you can refer to:GO TOUR slicing exercise to run the full record of the pit on the pit (from error report to successful operation) as shown in Figure 1

Error message:
go: downloading golang.org/x/tour v0.1.0
go: reading https://goproxy.io/golang.org/x/tour/@v/v0.1.0.zip: 502 Bad Gateway
Error cause:
- Default Go Module Agent
goproxy.ioTemporarily unavailable
Solution:
# 取消 GOPROXY,让 Go 直接访问官方源,重新运行
/app/go-tour/exercise-reader # unset GOPROXY
/app/go-tour/exercise-reader # go get golang.org/x/tour/reader
go: downloading golang.org/x/tour v0.1.0
/app/go-tour/exercise-reader # go run main.go
OK!
3. The second pit: the IDE still shows the import error as shown in Figure 2

Phenomenon:
- Terminal Execution
go run main.goOutput ‘OK!’ ✅ - vscode editor red wavy line hint:
Could not import golang.org/x/tour/reader❌
Cause analysis:
- IDE’s Language Server (GOPLS) caches old module states
- Module dependencies have been downloaded, but the IDE does not sync the latest information
Solution: As shown in Figure 3

# 方法:重新加载 VSCode 窗口(推荐)
# Ctrl+Shift+P → 输入 "Reload Window"
Fourth, the complete code example, the IDE will no longer display the import error as shown in Figure 4

package main
import "golang.org/x/tour/reader"
type MyReader struct{}
func (r MyReader) Read(b []byte) (int, error) {
for i := range b {
b[i] = 'A'
}
return len(b), nil
}
func main() {
reader.Validate(MyReader{})
}
5. Summary of experience
- Network problems: Domestic access to golang.org may need to adjust proxy settings
- IDE cache: After the command line is successful, remember to refresh the IDE’s language server
- Check sequence: make sure first
go runCan succeed, then deal with the IDE prompt problem