unknwon/the-way-to-go_ZH_CN · error
"Dial: " + err.Error()
Error message
"Dial: " + err.Error()
What it means
Panic in the WebSocket client (section 15.11): websocket.Dial("ws://localhost:12345/websocket", "", "http://localhost/") failure aborts with panic("Dial: " + err.Error()) before the readFromServer goroutine ever starts. The message embeds the dial error, typically 'connection refused' or an origin-handshake rejection.
Source
Thrown at eBook/15.11.md:58
}
}
```
示例 15.25 [websocket_client.go](examples/chapter_15/websocket_client.go)
```go
package main
import (
"fmt"
"time"
"websocket"
)
func main() {
ws, err := websocket.Dial("ws://localhost:12345/websocket", "",
"http://localhost/")
if err != nil {
panic("Dial: " + err.Error())
}
go readFromServer(ws)
time.Sleep(5e9)
ws.Close()
}
func readFromServer(ws *websocket.Conn) {
buf := make([]byte, 1000)
for {
if _, err := ws.Read(buf); err != nil {
fmt.Printf("%s\n", err.Error())
break
}
}
}
```
## 链接View on GitHub (pinned to 7a54d34d36)
Solutions
- Start the WebSocket server example first and confirm it is accepting on :12345 before running the client
- Match host, port, and path exactly with the server's handler route (/websocket)
- Use the same modern library (golang.org/x/net/websocket or gorilla/websocket) on both ends so the handshake matches
- Retry Dial a few times with a short backoff — dev servers routinely come up a beat after their clients
- Replace the panic with log.Fatal("Dial: ", err) for a clean, logged exit
Example fix
// before
ws, err := websocket.Dial("ws://localhost:12345/websocket", "", "http://localhost/")
if err != nil {
panic("Dial: " + err.Error())
}
// after
ws, err := websocket.Dial("ws://localhost:12345/websocket", "", "http://localhost/")
for i := 0; err != nil && i < 3; i++ {
time.Sleep(500 * time.Millisecond)
ws, err = websocket.Dial("ws://localhost:12345/websocket", "", "http://localhost/")
}
if err != nil {
log.Fatal("Dial: ", err)
} Defensive patterns
Strategy: validation
Validate before calling
// verify the server endpoint before the websocket handshake
if c, err := net.DialTimeout("tcp", "localhost:12345", time.Second); err != nil {
log.Fatal("websocket server not up on localhost:12345:", err)
} else {
c.Close()
}
ws, err := websocket.Dial("ws://localhost:12345/websocket", "", "http://localhost/") Try / catch
// bounded retry instead of panic on first failure
ws, err := websocket.Dial(addr, "", origin)
for i := 0; err != nil && i < 3; i++ {
time.Sleep(300 * time.Millisecond)
ws, err = websocket.Dial(addr, "", origin)
}
if err != nil {
log.Fatal("Dial: ", err)
} Prevention
- Start the server before the client; keep one shared constant for host, port, and path
- Keep the origin argument consistent with the dial host to pass origin checks
- Use the same websocket library/version on both ends so handshakes match
When it happens
Trigger: Dial returns an error: the server example is not running or listens elsewhere (port/path mismatch), the URL scheme is not ws://, or the third argument (origin 'http://localhost/') is rejected by the endpoint's origin check.
Common situations: Starting the client before the server (connection refused); server bound to 127.0.0.1 while the client reaches for another interface; port typos between the two listings; modern websocket libraries enforcing origin policies more strictly than the old 'websocket' package
Related errors
AI-assisted analysis of unknwon/the-way-to-go_ZH_CN@7a54d34d36 (2026-08-15).
Data as JSON: /api/errors/552950e64cc5ca0e.
Report an issue: GitHub.