func handleWebsocket(conn *websocket.Conn) { // Do something with websocket connection conn.Close() }
func main() { url := "wss://echo.websocket.org" conn, _, err := websocket.DefaultDialer.Dial(url, nil) if err != nil { log.Fatal("Error dialing websocket:", err) } defer conn.Close() // Use the websocket connection }This code establishes a Websocket connection with the URL "wss://echo.websocket.org". Once the connection is established, the `conn.Close()` method is deferred until the end of the function. This ensures that the connection will be closed when the function exits, even in the case of an error. Overall, the `websocket` package provides a comprehensive set of tools for working with Websockets in Go.