github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/doc/progs/error2.go (about) 1 // compile 2 3 // Copyright 2011 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // This file contains the code snippets included in "Error Handling and Go." 8 9 package main 10 11 import ( 12 "net/http" 13 "text/template" 14 ) 15 16 func init() { 17 http.HandleFunc("/view", viewRecord) 18 } 19 20 func viewRecord(w http.ResponseWriter, r *http.Request) { 21 c := appengine.NewContext(r) 22 key := datastore.NewKey(c, "Record", r.FormValue("id"), 0, nil) 23 record := new(Record) 24 if err := datastore.Get(c, key, record); err != nil { 25 http.Error(w, err.Error(), 500) 26 return 27 } 28 if err := viewTemplate.Execute(w, record); err != nil { 29 http.Error(w, err.Error(), 500) 30 } 31 } 32 33 // STOP OMIT 34 35 type ap struct{} 36 37 func (ap) NewContext(*http.Request) *ctx { return nil } 38 39 type ctx struct{} 40 41 func (*ctx) Errorf(string, ...interface{}) {} 42 43 var appengine ap 44 45 type ds struct{} 46 47 func (ds) NewKey(*ctx, string, string, int, *int) string { return "" } 48 func (ds) Get(*ctx, string, *Record) error { return nil } 49 50 var datastore ds 51 52 type Record struct{} 53 54 var viewTemplate *template.Template 55 56 func main() {}