main.go (708B)
1 //go:build example 2 3 package main 4 5 import ( 6 "log" 7 "net/http" 8 "os" 9 10 "go.lair.cx/functions/pkg/functions" 11 ) 12 13 func init() { 14 os.Stdout.Write([]byte("init\n")) 15 } 16 17 func main() { 18 functions.ServeFunc(ServeHTTP) 19 } 20 21 func ServeHTTP(w http.ResponseWriter, r *http.Request) { 22 name := r.URL.Query().Get("name") 23 if name == "" { 24 name = "world" 25 } 26 27 if name == "panic" { 28 panic("panic because name is panic") 29 } 30 31 if name == "redirect" { 32 http.Redirect(w, r, "/helloworld?name=redirected", http.StatusFound) 33 return 34 } 35 36 buf := make([]byte, 0, len(name)+8) 37 buf = append(buf, "Hello, "...) 38 buf = append(buf, name...) 39 buf = append(buf, "!\n"...) 40 41 _, err := w.Write(buf) 42 if err != nil { 43 log.Fatalln(err) 44 } 45 }