WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Expert · question 73 of 100

How can you implement real-time features in a Go application?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

Implementing real-time features in a Go application typically involves using WebSockets, which provides a bidirectional communication channel between a client (browser) and a server. This allows the server to push updates to the client as soon as new information becomes available.

Here’s a step-by-step guide on how to implement real-time features in a Go application using the Gorilla WebSocket package.

1. Install the Gorilla WebSocket package:

go get -u github.com/gorilla/websocket

2. Import Gorilla WebSocket package in your Go code:

import (
    "github.com/gorilla/websocket"
)

3. Define a WebSocket Upgrader:

var upgrader = websocket.Upgrader{
    ReadBufferSize:  1024,
    WriteBufferSize: 1024,
    CheckOrigin: func(r *http.Request) bool {
        return true // Allow connections from any origin.
    },
}

4. Handle incoming WebSocket connections in your HTTP handler:

func handleWebSocketConnection(w http.ResponseWriter, r *http.Request) {
    // Upgrade the HTTP connection to a WebSocket connection.
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Printf("Error upgrading to WebSocket: %v", err)
        return
    }
    defer conn.Close()

    // Handle incoming and outgoing WebSocket messages.
    for {
        messageType, message, err := conn.ReadMessage()
        if err != nil {
            log.Printf("Error reading message: %v", err)
            break
        }

        // Process the received message and send a response.
        err = conn.WriteMessage(messageType, []byte("Real-time response: "+string(message)))
        if err != nil {
            log.Printf("Error writing message: %v", err)
            break
        }
    }
}

5. Register the WebSocket handler in your HTTP server:

func main() {
    http.HandleFunc("/ws", handleWebSocketConnection)

    log.Fatal(http.ListenAndServe(":8080", nil))
}

Now you have a basic WebSocket server up and running using Go. To add real-time features, you can modify the message handling loop in the ‘handleWebSocketConnection‘ function.

For example, you can use channels to push updates to clients. Here’s a simple example where the server sends the current time to connected clients every second:

func handleWebSocketConnection(w http.ResponseWriter, r *http.Request) {
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Printf("Error upgrading to WebSocket: %v", err)
        return
    }
    defer conn.Close()

    // Create a channel to send updates to the client.
    updates := make(chan string)

    // Send the current time to the client every second.
    go func() {
        for {
            currentTime := time.Now().Format("15:04:05")
            updates <- "Current Time: " + currentTime
            time.Sleep(1 * time.Second)
        }
    }()

    // Listen for updates and send them to the client.
    for {
        select {
        case update := <-updates:
            err = conn.WriteMessage(websocket.TextMessage, []byte(update))
            if err != nil {
                log.Printf("Error writing message: %v", err)
                break
            }
        }
    }
}

In this example, a Goroutine is started for each connected client, which sends the current time to the client every second through the ‘updates‘ channel. The main loop listens for updates on the channel and writes them to the WebSocket connection, providing real-time updates to the client.

You can adapt this approach to handle various real-time scenarios, such as broadcasting updates to multiple clients, sending updates only when there’s new data, or customizing the update logic based on client inputs.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics