go test 工具 file with name endwith `_test.go` was build by `go test` instead of `go build`
there are three kind of Function need to be remember:
Function which Name starts with `Test` was used as UnitTest or FunctionalTest Function which Name starts with `Benchmark` was used as Benmark. Function which Name starts with ‘Example` was used as Example. Test Function test function must:
import `testing` package has `t *testing.T` as only argument use tool-function in `testing.
Jul 18, 2021
3 min read
introduce 在编译是不知道类型的情况下,可在运行时查看值,调用方法,更新变量,以及对布局进行操作的机制.
反射让我们把类型当作头等值
动态类型 vs 动态值
反射定义了两个重要的类型 reflect.Type, reflect.Value
reflect.Type 类型描述符 接口值的动态类型也是类型描述符
满足fmt.Stringer接口
t := reflect.TypeOf(3) fmt.Println(t.String()) fmt.Println(t) // 反射总是返回具体类型 var w io.Writer = os.Stdout fmt.Println(reflect.TypeOf(w)) // return os.File int int *os.File reflect.Value v := reflect.ValueOf(3) fmt.Println(v) // this will handle reflect.Value fmt.Println("%v\n", v) // implement the fmt.Stringer interface, but only return `Type` if not string fmt.Println(v.String()) // get `Value` Type t := v.Type() fmt.Println(t.String()) // the reverse operator x := v.Interface() // return `interface{}` i := x.
Jul 18, 2021
6 min read
package main import ( "fmt" "http" "reflect" ) // search ... func search(resp http.ResponseWriter, req *http.Request) { var data struct { Labels []string `http:"1"` MaxResults int `http:"max"` Exact bool `http:"x"` } data.MaxResults = 10 if err := Unpack(req, &data); err != nil { http.Error(resp, err.Error(), http.StatusBadRequest) return } fmt.Fprintf(resp, "Search: %+v\n", data) } // Unpack func Unpack(req *http.Request, ptr interface{}) error { if err := req.ParseForm(); err != nil { return err } fields := make(map[string]reflect.
Jul 18, 2021
1 min read