15. io, os and net/http - Getting started
15.1 io
Usage: import "io"
Package io provides basic interfaces to I/O primitives.
func Copy(dst Writer, src Reader) (written int64, err error)
Copy copies from src to dst until either EOF is reached on src or an error occurs. It returns the number of bytes copied and the first error encountered while copying, if any.
A successful Copy returns err == nil, not err == EOF. Because Copy is defined to read from src until EOF, it does not treat an EOF from Read as an error to be reported.
15.2 os
Usage: import "os"
Package os provides a platform-independent interface to operating system functionality.
Stdin, Stdout, and Stderr are open Files pointing to the standard input, standard output, and standard error file descriptors.
Hence os.Stdout will write to the standard output.
15.3 TCP and net/http
Networking usually implies TCP/IP, the way in which millions of machines communicate back and forth across the Internet. Network communication is conceptualized at different layers - data link, network, transport and application. The application layer is the world of telnet, FTP, email protocols, and much more.
15.3.1 Basic Networking
Let us talk a little bit about basic networking.
Our discussion of networking focuses on both sides of a client-server relationship. The client requests that some action be performed, and the server performs the action and responds to the client. A dedicated server spends its lifetime waiting for messages and answering them. A common implementation of the request-response model is between World Wide Web browsers and World Wide Web servers. When a user selects a Web site to browse through a browser (the client application), a request is sent to the appropriate Web server (the server application). The server normally responds to the client by sending an appropriate HTML Web page.
15.3.2 Port
A port is not a physical device, but an abstraction to facilitate communication between a server and a client.
Ports are described by a 16-bit integer value. Hence, a machine can have a maximum of 65536 port numbers (ranging from 0 to 65535). The port numbers are divided into three ranges: the Well Known Ports, the Registered Ports, and the Dynamic and/or Private Ports. The Well Known Ports are those from 0 through 1023 (for example, port no. 80 is for http, port no. 25 is for smtp and so on). The Registered Ports are those from 1024 through 49151. The Dynamic and/or Private Ports are those from 49152 through 65535.
15.3.3 Internet Addresses
These are the numerical host addresses that consist of four bytes such as 132.163.4.102. The IP address 127.0.0.1 (localhost) is a special address, called the local loopback address, which denotes the local machine.
15.3.4 net/http
Usage: import "net/http"
Package http provides HTTP client and server implementations.
Get, Head, Post, and PostForm make HTTP (or HTTPS) requests.
func Get(url string) (resp *Response, err error)
Get issues a GET to the specified URL. An error is returned if there was an HTTP protocol error. When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.
The Response2 structure among others has the field StatusCode which is an int (e.g. 200), a field Body that represents the response body and Close which should be called by the caller to close the Body.
The http.StatusOK is a constant with the value of 200.
An example:
1 package main
2
3 import (
4 "io"
5 "log"
6 "net/http"
7 "os"
8 )
9
10 func main() {
11 resp, err := http.Get("http://www.google.com/robots.txt")
12 if err != nil {
13 log.Fatal(err)
14 }
15
16 defer resp.Body.Close()
17
18 if resp.StatusCode != http.StatusOK {
19 log.Fatal(resp.Status)
20 }
21
22 _, err = io.Copy(os.Stdout, resp.Body)
23 if err != nil {
24 log.Fatal(err)
25 }
26 }