Creating a server

Here is an example server. The tcp argument for net.Listen() can be changed to udp if you want to change protocol:

package mainimport (   "net"   "fmt"   "log")var protocol = "tcp" // tcp or udpvar listenAddress = "localhost:3000"func main() {   listener, err := net.Listen(protocol, listenAddress)   if err != nil {      log.Fatal("Error creating listener. ", err)   }   log.Printf("Now listening for connections.")   for {      conn, err := listener.Accept()      if err != nil {         log.Println("Error accepting connection. ", err)      }      go handleConnection(conn)   }}func handleConnection(conn net.Conn) {   incomingMessageBuffer := make([]byte, 4096)   numBytesRead, err := conn.Read(incomingMessageBuffer)   if err != nil {      log.Print("Error reading from client. ", err)

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.