Commit c9d0758f authored by Kirill Smelkov's avatar Kirill Smelkov

go: Minor changes in the code to follow code conventions

Merge fixes to code formatting and naming as reported by gofmt and
golint from Gabriel.

see: kirr/helloweb!1
parents 0487fa7b 63f6d9d3
...@@ -4,22 +4,20 @@ ...@@ -4,22 +4,20 @@
package main package main
import ( import (
"os" "flag"
"fmt" "fmt"
"time"
"log" "log"
"flag"
"strings"
"net" "net"
"net/http" "net/http"
"os"
"strings"
"time"
) )
func asctime() string { func asctime() string {
return time.Now().Format(time.ANSIC) return time.Now().Format(time.ANSIC)
} }
// wrapper for http.Handler to log all requests // wrapper for http.Handler to log all requests
func logit(handler http.Handler) http.Handler { func logit(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
...@@ -28,8 +26,6 @@ func logit(handler http.Handler) http.Handler { ...@@ -28,8 +26,6 @@ func logit(handler http.Handler) http.Handler {
}) })
} }
var name string var name string
func webhello(w http.ResponseWriter, r *http.Request) { func webhello(w http.ResponseWriter, r *http.Request) {
...@@ -40,7 +36,7 @@ func webhello(w http.ResponseWriter, r *http.Request) { ...@@ -40,7 +36,7 @@ func webhello(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
logfile := flag.String("logfile", "", "log output to file instead of stderr") logfile := flag.String("logfile", "", "log output to file instead of stderr")
flag.Usage = func() { flag.Usage = func() {
fmt.Println("Usage: helloweb.go [options] bind_ip bind_port ...") fmt.Println("Usage: helloweb.go [options] bindIP bindPort ...")
flag.PrintDefaults() flag.PrintDefaults()
} }
flag.Parse() flag.Parse()
...@@ -50,9 +46,9 @@ func main() { ...@@ -50,9 +46,9 @@ func main() {
os.Exit(1) os.Exit(1)
} }
bind_ip := flag.Arg(0) bindIP := flag.Arg(0)
bind_port := flag.Arg(1) bindPort := flag.Arg(1)
bind_addr := net.JoinHostPort(bind_ip, bind_port) bindAddr := net.JoinHostPort(bindIP, bindPort)
name = strings.Join(flag.Args()[2:], " ") name = strings.Join(flag.Args()[2:], " ")
if name == "" { if name == "" {
...@@ -61,7 +57,7 @@ func main() { ...@@ -61,7 +57,7 @@ func main() {
// redirect log to file, if requested // redirect log to file, if requested
if *logfile != "" { if *logfile != "" {
f, err := os.OpenFile(*logfile, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0644) f, err := os.OpenFile(*logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil { if err != nil {
panic(err) panic(err)
} }
...@@ -69,9 +65,9 @@ func main() { ...@@ -69,9 +65,9 @@ func main() {
log.SetOutput(f) log.SetOutput(f)
} }
log.Printf("* %s helloweb.go starting at %s", asctime(), bind_addr) log.Printf("* %s helloweb.go starting at %s", asctime(), bindAddr)
http.HandleFunc("/", webhello) http.HandleFunc("/", webhello)
log.Fatal( log.Fatal(
http.ListenAndServe(bind_addr, logit(http.DefaultServeMux))) http.ListenAndServe(bindAddr, logit(http.DefaultServeMux)))
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment