listener, err := net.Listen("tcp", ":8080") if err != nil { log.Fatal("Error listening: ", err.Error()) } defer listener.Close() for { conn, err := listener.AcceptTCP() if err != nil { log.Fatal("Error accepting: ", err.Error()) } // handle incoming connection in a separate goroutine go handleConnection(conn) }
listener, err := net.Listen("tcp", ":8080") if err != nil { log.Fatal("Error listening: ", err.Error()) } defer listener.Close() conn, err := listener.AcceptTCP() if err != nil { log.Fatal("Error accepting: ", err.Error()) } // read data from the connection buf := make([]byte, 1024) _, err = conn.Read(buf) if err != nil { log.Fatal("Error reading: ", err.Error()) }In this example, we listen on port 8080 for one incoming TCP connection, accept the connection using AcceptTCP, and read data from the connection using conn.Read.