Go笔记

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// https://stackoverflow.com/questions/33426977/how-to-golang-check-a-variable-is-nil
package main

import (
    "bytes"
    "net/http"
)


func main() {
    client := &http.Client{}
    var content *bytes.Reader
    content = nil
    // it's error
    // req, _ := http.NewRequest("GET", "http://www.github.com", content)
    // it's ok
    req, _ := http.NewRequest("GET", "http://www.github.com", nil)
    resp, _ := client.Do(req)
    defer resp.Body.Close()
}


// A go interface consists of a type and a value. An interface is only nil if both the type and the value are nil. You provided a type but no value: Therefore NewRequest tried to call Read on a nil struct (the value of the interface).
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func (c Chain) Then(h http.Handler) http.Handler {
	if h == nil {
		h = http.DefaultServeMux
	}

	for i := range c.constructors {
		h = c.constructors[len(c.constructors)-1-i](h)
	}

	return h
}
// Middleware1(Middleware2(Middleware3(App)))
Licensed under CC BY-NC-SA 4.0
Built with Hugo
主题 StackJimmy 设计