Skip to content

Instantly share code, notes, and snippets.

@Yapcheekian
Created May 25, 2021 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yapcheekian/81853f2d39327c86c8bc24c1d05c2490 to your computer and use it in GitHub Desktop.
Save Yapcheekian/81853f2d39327c86c8bc24c1d05c2490 to your computer and use it in GitHub Desktop.
example server in go
package main
import "net"
import "fmt"
import "bufio"
import "strings" // only needed below for sample processing
func main() {
fmt.Println("Launching server...")
// listen on all interfaces
ln, _ := net.Listen("tcp", ":8081")
// accept connection on port
conn, _ := ln.Accept()
// run loop forever (or until ctrl-c)
for {
// will listen for message to process ending in newline (\n)
message, _ := bufio.NewReader(conn).ReadString('\n')
// output message received
fmt.Print("Message Received:", string(message))
// sample process for string received
newmessage := strings.ToUpper(message)
// send new string back to client
conn.Write([]byte(newmessage + "\n"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment