Go’s standard library is a rich collection of packages bundled with the language, covering work from string manipulation to HTTP servers. Reusing these packages speeds development and favors code that is tested, runtime-integrated, efficient, and familiar to other Go developers. section 1.6
Prefer standard-library packages when they meet the need because they provide a common foundation for practical programs:
- Build cross-platform, efficient command-line tools with standard-library support. section 1.2
- Create clients and servers with built-in HTTP and TCP networking support. section 1.2
- Use packages such as testing for automated tests, flag for command-line flags, and http for requests and services. section 1.6
- Add third-party packages through Go’s module system when everyday standard-library facilities do not cover specialized or advanced needs. section 1.6
A minimal HTTP server
The net/http package can listen on localhost:8080 and reply “hello, gophers!” to every request. The server serves requests concurrently with goroutines, showing how a small standard-library program can form a useful network service. section 1.6 section 8.3
package main
import "net/http" #1
func main() {
handler := func( #2
w http.ResponseWriter, #3
r *http.Request, #4
) {
w.Write([]byte("hello, gophers!")) #5
}
http.ListenAndServe("localhost:8080", http.HandlerFunc(handler))
}
Inspect the complete server: a handler receives a ResponseWriter and Request, writes the response body, and ListenAndServe starts the server. section 1.6
For more control, an http.Handler implementation supplies ServeHTTP. The server calls it with an http.ResponseWriter and *http.Request; set headers and status before writing the response body. section 8.3






