没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Welcome to Building Web Apps with Go! If you are reading this then you have just started your journey from noob to pro.
No seriously, web programming in Go is so fun and easy that you won't even notice how much information you are
learning along the way!
Keep in mind that there are still portions of this book that are incomplete and need some love. The beauty of open source
publishing is that I can give you an incomplete book and it is still of value to you.
Before we get into all the nitty gritty details, let's start with some ground rules:
To keep this tutorial small and focused, I'm assuming that you are prepared in the following ways:
1. You have installed the Go Programming Language.
2. You have setup a GOPATH by following the How to Write Go Code tutorial.
3. You are somewhat familiar with the basics of Go. (The Go Tour is a pretty good place to start)
4. You have installed all the required packages
5. You have installed the Heroku Toolbelt
6. You have a Heroku account
For the most part we will be using the built in packages from the standard library to build out our web apps. Certain
lessons such as Databases, Middleware, URL Routing, and Controllers will require a third party package. Here is a list of
all the go packages you will need to install before starting:
Name Import Path
Gorilla Mux github.com/gorilla/mux
Negroni github.com/codegangsta/negroni
Controller github.com/codegangsta/controller
Black Friday github.com/russross/blackfriday
Render gopkg.in/unrolled/render.v1
SQLite3 github.com/mattn/go-sqlite3
You can install (or update) these packages by running the following command in your console
go get -u <import_path>
For instance, if you wish to install Negroni, the following command would be:
go get -u github.com/codegangsta/negroni
Introduction
Prerequisites
Required Packages
If you have built a web application before, you surely know that there are quite a lot of concepts to keep in your head.
HTTP, HTML, CSS, JSON, databases, sessions, cookies, forms, middleware, routing and controllers are just a few among
the many things your web app may need to interact with.
While each one of these things can be important in the building of your web applications, not every one of them is
important for any given app. For instance, a web API may just use JSON as it's serialization format, thus making concepts
like HTML not relevant for that particular web app.
The Go community understands this dilemma. Rather than rely on large, heavyweight frameworks that try to cover all the
bases, Go programmers pull in the bare necessities to get the job done. This minimalist approach to web programming
may be off-putting at first, but the result of this effort is a much simpler program in the end.
Go makes things simple, it's as easy as that. If we train ourselves to align with the "Go way" of programming for the web,
we will end up with more simple, flexible, and maintainable web applications.
As we go through the exercises in this book, I think you will be surprised by how simple some of these programs can be
whilst still affording a bunch of functionality.
When sitting down to craft your own web applications in Go, think hard about the components and concepts that your app
will be focused on, and use just those pieces. This book will be covering a wide array of web topics, but do not feel
obligated to use them all. In the words of our friend Lonestar, "Take only what you need to survive".
Go Makes Things Simple
The Go Way
Power in Simplicity
You have probably heard that Go is fantastic for building web applications of all shapes and sizes. This is partly due to
the fantastic work that has been put into making the standard library clean, consistent, and easy to use.
Perhaps one of the most important packages for any budding Go web developer is the net/http package. This package
allows you to build HTTP servers in Go with it's powerful compositional constructs. Before we start coding, let's do an
extremely quick overview of HTTP.
When we talk about building web applications, we usually mean that we are building HTTP servers. HTTP is a protocol
that was originally designed to transport HTML documents from a server to a client web browser. Today, HTTP is used to
transport a whole lot more than HTML.
The important thing to notice in this diagram is the two points of interaction between the Server and the Browser. The
Browser makes an HTTP request with some information, the Server then processes that request and returns a Response.
This pattern of request-response is one of the key focal points in building web applications in Go. In fact, the net/http
package's most important piece is the http.Handler Interface.
As you become more familiar with Go, you will notice how much of an impact interfaces make in the design of your
programs. The net/http interface encapsulates the request-response pattern in one method:
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
The net/http Package
HTTP Basics
The http.Handler Interface
Implementors of this interface are expected to inspect and process data coming from the http.Request object and write out
a response to the http.ResponseWriter object.
The http.ResponseWriter interface looks like this:
type ResponseWriter interface {
Header() Header
Write([]byte) (int, error)
WriteHeader(int)
}
Because much of the net/http package is built off of well defined interface types, we can (and are expected to) build our
web applications with composition in mind. Each http.Handler implementation can be thought of as it's own web server.
Many patterns can be found in that simple but powerful assumption. Throughout this book we will cover some of these
patterns and how we can use them to solve real world problems.
Let's solve a real world problem in 1 line of code.
Most of the time people just need to serve static files. Maybe you have a static HTML landing page and just want to serve
up some HTML, images, and CSS and call it a day. Sure, you could pull in apache or Python's SimpleHTTPServer , but
Apache is too much for this little site and SimpleHTTPServer is, well, too slow.
We will begin by creating a new project in our GOPATH .
cd GOPATH/src
mkdir fileserver && cd fileserver
Create a main.go with our typical go boilerplate.
package main
import "net/http"
func main() {
}
All we need to import is the net/http package for this to work. Remember that this is all part of the standard library in Go.
Let's write our fileserver code:
http.ListenAndServe(":8080", http.FileServer(http.Dir(".")))
The http.ListenAndServe function is used to start the server, it will bind to the address we gave it ( :8080 ) and when it
receives an HTTP request, it will hand it off to the http.Handler that we supply as the second argument. In our case it is the
built-in http.FileServer .
The http.FileServer function builds an http.Handler that will serve an entire directory of files and figure out which file to
serve based on the request path. We told the FileServer to serve the current working directory with http.Dir(".") .
Composing Web Services
Exercise: 1 Line File Server
剩余22页未读,继续阅读
资源评论
whomwhomwhom
- 粉丝: 5
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功