basics/

directory
v0.0.0-...-86674b3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2017 License: Apache-2.0

README

Web - Basics

Learn the basics of building web services and applications in Go.

Notes

  • The standard library has much of what you need to build services and apps.
  • The http package provides the building blocks. The most important types to learn right now are these
// A Request represents an HTTP request received by a server
// or to be sent by a client.
//
// Several fields are hidden from this example.
type Request struct {
	Method string
	URL    *url.URL
	Header Header
	Body   io.ReadCloser
}

// A ResponseWriter interface is used by an HTTP handler to
// construct an HTTP response.
type ResponseWriter interface {
	Header() Header
	Write([]byte) (int, error)
	WriteHeader(int)
}

// A Handler responds to an HTTP request.
type Handler interface {
	ServeHTTP(ResponseWriter, *Request)
}

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)

// ListenAndServe listens on the TCP network address addr
// and then calls Serve with handler to handle requests
// on incoming connections.
func ListenAndServe(addr string, handler Handler) error

https://golang.org/pkg/net/http/
https://golang.org/doc/articles/wiki/

Code Review

Basic Web Handler
Routing Handlers
Using the Default Mux
Making Handlers out of Functions
Closures as HandlerFuncs
Servers are already Concurrent

Notes

An HTTP Request Request

The Response

Response

Exercises

Exercise 1

Write a simple web service that has a set of different routes that return the string "Hello World" in multiple languages. Build the service using an Application context (example4) that will own the different handler methods. Then create your own mux (example2), bind the routes and start the service. Validate your routes work in your browser.


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
Sample program to show how to create a handler for a basic web app.
Sample program to show how to create a handler for a basic web app.
Sample program to show how to create handlers for different routes.
Sample program to show how to create handlers for different routes.
Sample program to show how to create handlers for different routes utilizing the default mux.
Sample program to show how to create handlers for different routes utilizing the default mux.
Sample program to show how to use methods as
Sample program to show how to use methods as
Sample program to show how to create handlers out of any function using http.HandlerFunc.
Sample program to show how to create handlers out of any function using http.HandlerFunc.
Sample program to show how http servers already handle concurrent requests.
Sample program to show how http servers already handle concurrent requests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL