TCP server
Context
Section titled “Context”For low‑level network protocols, use the net package to create TCP servers and clients.
Example
Section titled “Example”A TCP echo server.
Code example
Section titled “Code example”package main
import ( "bufio" "fmt" "net")
func handle(conn net.Conn) { defer conn.Close() scanner := bufio.NewScanner(conn) for scanner.Scan() { fmt.Fprintln(conn, "echo:", scanner.Text()) }}
func main() { ln, err := net.Listen("tcp", ":8080") if err != nil { fmt.Println("Error:", err) return } defer ln.Close() for { conn, err := ln.Accept() if err != nil { continue } go handle(conn) }}Output (telnet localhost 8080)
Section titled “Output (telnet localhost 8080)”helloecho: hello