Where to put “defer req.Body.Close()”?
A request body does not need to be closed in the handler. From the http.Request documentation // The Server will close the request body. The ServeHTTP // Handler does not need to.
A request body does not need to be closed in the handler. From the http.Request documentation // The Server will close the request body. The ServeHTTP // Handler does not need to.
strings.EqualFold() can check if two strings are equal, while ignoring case. It even works with Unicode. See http://golang.org/pkg/strings/#EqualFold for more info. http://play.golang.org/p/KDdIi8c3Ar package main import ( “fmt” “strings” ) func main() { fmt.Println(strings.EqualFold(“HELLO”, “hello”)) fmt.Println(strings.EqualFold(“ÑOÑO”, “ñoño”)) } Both return true.
You probably have dependencies that are being recompiled each time. Try go install -a mypackage to rebuild all dependencies. Removing $GOPATH/pkg also helps to ensure you don’t have old object files around. Building with the -x flag will show you if the toolchain is finding incompatible versions.
Just create a wrapper, it’s rather easy in Go: func HomeHandler(response http.ResponseWriter, request *http.Request) { fmt.Fprintf(response, “Hello home”) } func Middleware(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Println(“middleware”, r.URL) h.ServeHTTP(w, r) }) } func main() { r := mux.NewRouter() r.HandleFunc(“https://stackoverflow.com/”, HomeHandler) http.Handle(“https://stackoverflow.com/”, Middleware(r)) }
Here is a very simple example, that illustrates how & and * are used. Note that * can be used for two different things 1) to declare a variable to be a pointer 2) to dereference a pointer. package main import “fmt” func main() { b := 6 var b_ptr *int // *int is used …
Read http://golang.org/pkg/image // you need the image package, and a format package for encoding/decoding import ( “bytes” “image” “image/jpeg” // if you don’t need to use jpeg.Encode, use this line instead // _ “image/jpeg” “github.com/nfnt/resize” ) // Decoding gives you an Image. // If you have an io.Reader already, you can give that to Decode …
Go 1.8 and above: sort.Slice(timeSlice, func(i, j int) bool { return timeSlice[i].date.Before(timeSlice[j].date) })
Interestingly enough, Rob Pike just proposed (18 hours ago) the library filter which does a bit what you want: See for instance Choose() // Choose takes a slice of type []T and a function of type func(T) bool. (If // the input conditions are not satisfied, Choose panics.) It returns a newly // allocated slice …
I’m sorry to not answer your question directly: IMHO, the best way how to implement recursive locks in Go is to not implement them, but rather redesign your code to not need them in the first place. It’s probable, I think, that the desire for them indicates a wrong approach to some (unknown here) problem …
The Go runtime and standard libraries do not provide goroutine local storage or a goroutine identifier that can be used to implement goroutine local storage. The third party gls package implements goroutine local storage in an interesting way. Some find this package horrifying and others think it’s clever. The Go team recommends passing context explicitly …