import "golang.org/x/net/websocket" func handler(ws *websocket.Conn) { // get the remote address of the connection remoteAddr := ws.RemoteAddr() fmt.Printf("Remote address: %s\n", remoteAddr) // rest of the code }
package main import ( "fmt" "golang.org/x/net/websocket" "net/http" ) func greetingHandler(ws *websocket.Conn) { greeting := []byte("Hello, Sir!") _, err := ws.Write(greeting) if err != nil { fmt.Printf("Error: %v\n", err) } // get the remote address of the connection remoteAddr := ws.RemoteAddr() fmt.Printf("Remote address: %s\n", remoteAddr) } func main() { http.Handle("/", websocket.Handler(greetingHandler)) err := http.ListenAndServe(":8080", nil) if err != nil { fmt.Printf("Error: %v\n", err) } }In this example, `RemoteAddr()` is used to fetch the remote address of the WebSocket connection in the `greetingHandler()` function. The function sends a message "Hello, Sir!" to the WebSocket connection when it is established.