Sobes.tech
Junior — Middle

Have you had experience working with sockets for network communication?

sobes.tech AI

Answer from AI

Yes, experience with sockets for network interaction often includes creating client and server applications that exchange data over a network.

In Go, the net package is commonly used for socket programming. For example, you can create a simple TCP server and client.

Example of a TCP server in Go:

package main

import (
    "fmt"
    "net"
)

func main() {
    ln, err := net.Listen("tcp", ":8080")
    if err != nil {
        panic(err)
    }
    defer ln.Close()

    for {
        conn, err := ln.Accept()
        if err != nil {
            fmt.Println("Connection error:", err)
            continue
        }
        go handleConnection(conn)
    }
}

func handleConnection(conn net.Conn) {
    defer conn.Close()
    buf := make([]byte, 1024)
    n, err := conn.Read(buf)
    if err != nil {
        fmt.Println("Read error:", err)
        return
    }
    fmt.Println("Received message:", string(buf[:n]))
    conn.Write([]byte("Accepted"))
}

This experience demonstrates an understanding of socket-level network interaction, connection management, and data processing.