Skip to content

Instantly share code, notes, and snippets.

@amy
Last active September 25, 2016 16:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amy/060284019774d5363145529caedecd38 to your computer and use it in GitHub Desktop.
Save amy/060284019774d5363145529caedecd38 to your computer and use it in GitHub Desktop.
/* 1 */
So I'm reading the documentation for database/sql for rows.Close():
"Close closes the Rows, preventing further enumeration. If Next returns false, the Rows are closed automatically
and it will suffice to check the result of Err. Close is idempotent and does not affect the result of Err.""
I still don't get what closing a row does. I get closing in the context of closing for like DB.close() since its a DB
connection. But what does closing a row mean. "Close closes the Rows". Its using the same word to describe itself.
Answer: DB Connection Pooling
Open is essentially only there to store credentials & never makes a connection.
Queries / Insert / etc. each create separate connections in the DB Connection Pool as they are independent of
each other.
/* 2 */
Defer is LIFO
When should I use defer?
> Closing resources
> Debugging
> recovering panics
> Seeing how long a function's execution time is
> Mutex unlocking (mutex.Lock() defer mutex.Unlock())
/* 3 */
Shadowing:
package main
func demo() error {
return nil
}
func main() {
err := demo() // we don't handle this error
err = demo() // and we skip to this line
if err != nil {
panic(err)
}
}
/* 4 */
I had a handler working yesterday for a PUT request. What happened today?
I was using different types of bodies. Yesterday was using URL-encoded, so FormValue worked. Today I'm using a
JSON body for the put request... and it doesn't work?
Answer: Url-encoded had a body that was of the form-encoding type. So FormValue can decode it with ParseForm.
JSON bodies are a JSON object. So I need to decode it: json.NewDecoder(r.Body).Decode(&data)
/* 5 */
How to I pass pointers to structs that implement an interface as a parameter in functions / methods?
func foo(i SomeInterface){
i.method()
}
func main {
a := &SomeStruct{} // assume that SomeStruct implements method()
foo(a)
}
/* 5a */
How does a pointer to a struct that implements an interface satisfy the interface?
"The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T
(that is, it also contains the method set of T)"
https://golang.org/ref/spec#Method_sets
/* 6 */
How do I compare that the field values of two structs are equal?
Just compare them (==, !=). HOWEVER, all field values must be comparable types.
Ex: slices are NOT comparable (sad) ==> reflect.DeepEqual
/* 7 */
Explore ServeHTTP more. Don't have a question. Just general confusion. The documentation says that its a wrapper
for f(w,r). Have a general idea of what it is from examples.
"ServeHTTP should write reply headers and data to the ResponseWriter and then return. Returning signals
that the request is finished; it is not valid to use the ResponseWriter or read from the Request.Body
after or concurrently with the completion of the ServeHTTP call."
"The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a
function with the appropriate signature, HandlerFunc(f) is a Handler that calls f."
Summary: Writes/reads to/from appropriate streams from the http request.
1.) func FooHandler(w http.ResponseWriter, r *http.Request){
// Issue: No dependency injection of DB connection.
// Lol. Good luck with testing
}
2.) type FoodHandler struct {
// Now you can swap out mocks
db *sql.DB
}
func (h *FooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Query
db.Exec("YOUR QUERY HERE")
// Write json of e back to w (the client)
e := Event{name: "foo", description: "bar"}
json.NewEncoder(w).Encode(e)
}
3.) func helloHandler(db *sql.DB) http.Handler {
// Type cast the anonymous function to type HandlerFunc
// HandlerFunc's ServeHttp method will call the anonymous function
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Query
db.Exec("YOUR QUERY HERE")
// Write json of e back to w (the client)
e := Event{name: "foo", description: "bar"}
json.NewEncoder(w).Encode(e)
})
} // this removes unnecessarily defining a struct per handler like in 2.)
/* 8 */
How do I use sort.Reverse()? I just want the reverse and not the sorted reverse.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment