it-ebooks - building-web-apps-with-go
Here you can read online it-ebooks - building-web-apps-with-go full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2016, publisher: iBooker it-ebooks, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:
Romance novel
Science fiction
Adventure
Detective
Science
History
Home and family
Prose
Art
Politics
Computer
Non-fiction
Religion
Business
Children
Humor
Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.
building-web-apps-with-go: summary, description and annotation
We offer to read an annotation, description, summary or preface (depends on what the author of the book "building-web-apps-with-go" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.
building-web-apps-with-go — read online for free the complete book (whole text) full work
Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "building-web-apps-with-go" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.
Font size:
Interval:
Bookmark:
- 7.1
- 7.2
- 7.3
- 8.1
- 8.2
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:
- You have installed the Go Programming Language.
- You have setup a
GOPATH
by following the How to Write Go Code tutorial. - You are somewhat familiar with the basics of Go. (The Go Tour is a pretty good place to start)
- You have installed all the
- You have installed the Heroku Toolbelt
- 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 and URL Routing 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 | Description |
---|---|---|
httprouter | github.com/julienschmidt/httprouter | A high performance HTTP request router that scales well |
Negroni | github.com/codegangsta/negroni | Idiomatic HTTP Middleware |
Black Friday | github.com/russross/blackfriday | a markdown processor |
Render | gopkg.in/unrolled/render.v1 | Easy rendering for JSON, XML, and HTML |
SQLite3 | github.com/mattn/go-sqlite3 | sqlite3 driver for go |
You can install (or update) these packages by running the following command in your console
go get -u
For instance, if you wish to install Negroni, the following command would be:
go get -u github.com/codegangsta/negroni
If you have built a web application before, you surely know that there arequite a lot of concepts to keep in your head. HTTP, HTML, CSS, JSON, databases,sessions, cookies, forms, middleware, routing and controllers are just a fewamong 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 webapplications, not every one of them is important for any given app. Forinstance, a web API may just use JSON as its serialization format, thus makingconcepts 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 inthe bare necessities to get the job done. This minimalist approach to webprogramming may be off-putting at first, but the result of this effort is a muchsimpler program in the end.
Go makes things simple, it's as easy as that. If we train ourselves toalign with the "Go way" of programming for the web, we will end up with moresimple, flexible, and maintainable web applications.
As we go through the exercises in this book, I think you will be surprised byhow simple some of these programs can be whilst still affording a bunch offunctionality.
When sitting down to craft your own web applications in Go, think hard aboutthe components and concepts that your app will be focused on, and use justthose pieces. This book will be covering a wide array of web topics, but do notfeel obligated to use them all. In the words of our friend Lonestar, "Takeonly what you need to survive".
If you want rendering JSON and HTML to be even simpler, there is thegithub.com/unrolled/render
package. This package was inspired by themartini-contrib/render
package and is my goto when it comes to rendering datafor presentation in my web applications.
package main import ( "net/http" "gopkg.in/unrolled/render.v1" ) func main() { r := render.New(render.Options{}) mux := http.NewServeMux() mux.HandleFunc( "/" , func (w http.ResponseWriter, req *http.Request) { w.Write([] byte ( "Welcome, visit sub pages now." )) }) mux.HandleFunc( "/data" , func (w http.ResponseWriter, req *http.Request) { r.Data(w, http.StatusOK, [] byte ( "Some binary data here." )) }) mux.HandleFunc( "/json" , func (w http.ResponseWriter, req *http.Request) { r.JSON(w, http.StatusOK, map [ string ] string { "hello" : "json" }) }) mux.HandleFunc( "/html" , func (w http.ResponseWriter, req *http.Request) { // Assumes you have a template in ./templates called "example.tmpl" // $ mkdir -p templates && echo "
Hello {{.}}.
Exercises
Have fun playing with all of the options available when calling
render.New()
- Try using the
.yield
helper function (with the curly braces) and a layout with HTML templates.
Testing is an important part of any application.There are two approaches we can take to testing Go web applications.The first approach is a unit-test style approach. The other is more ofan end-to-end approach. In this chapter we'll cover both approaches.
Unit testing allows us to test a http.HandlerFunc
directly withoutrunning any middleware, routers, or any other type of code that mightotherwise wrap the function.
package main import ( "fmt" "net/http" ) func HelloWorld(res http.ResponseWriter, req *http.Request) { fmt.Fprint(res, "Hello World" )} func main() { http.HandleFunc( "/" , HelloWorld) http.ListenAndServe( ":3000" , nil )}
This is the test file. It should be placed in the same directory asyour application and name main_test.go
.
package main import ( "net/http" "net/http/httptest" "testing" ) func Test_HelloWorld(t *testing.T) { req, err := http.NewRequest( "GET" , "http://example.com/foo" , nil ) if err != nil { t.Fatal(err) } res := httptest.NewRecorder() HelloWorld(res, req) exp := "Hello World" act := res.Body.String() if exp != act { t.Fatalf( "Expected %s gog %s" , exp, act) }}
- Change the output of
HelloWorld
to print a parameter and then test that the parameter is rendered. - Create a POST request and test that the request is properly handled.
End to end allows us to test applications through the whole request cycle.Where unit testing is meant to just test a particular function, end to endtests will run the middleware, router, and other that a request my passthrough.
Font size:
Interval:
Bookmark:
Similar books «building-web-apps-with-go»
Look at similar books to building-web-apps-with-go. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.
Discussion, reviews of the book building-web-apps-with-go and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.