ln, err := net.Listen("tcp", ":8080") if err != nil { // handle error } defer ln.Close() for { conn, err := ln.Accept() if err != nil { // handle error } // handle connection }
resp, err := http.Get("https://example.com") if err != nil { // handle error } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { // handle error } fmt.Println(string(body))In this example, we use the net/http package to send an HTTP GET request to the example.com website and read the response body. Both examples use the net package and its interfaces to establish and maintain network connections.