go 学习笔记

// 声明 type struct对象 interface接口 map数组
type Dictionary interface{}
type Dictionary struct{}
type Dictionary map[string]string

//返回错误
errors.New("xxx")

//变量直接声明,可直接调用
var ErrNotFound = errors.New("xx")

//map 查找的有趣特性。它可以返回两个值。第二个值是一个布尔值,表示是否成功找到 key。
definition, ok := d[k]

//作用:fmt.Println或打印一个变量的值的时候,会判断这个变量是否实现了Stringer接口,如果实现了,则调用这个变量的String()方法,并将返回值打印到屏幕上
type Stringer interface {
String() string
}
//使用
func (a Animal) String() string {
return fmt.Sprintf("%d xxx", a)
}

// Map 有一个有趣的特性,不使用指针传递你就可以修改它们。这是因为 map 是引用类型。这意味着它拥有对底层数据结构的引用,就像指针一样。它底层的数据结构是 hash table 或 hash map
dictionary = map[string]string{}
dictionary = make(map[string]string)

//基准测试 Benchmark开头
func BenchmarkDemo(b *testing.B) {
for i := 0; i < b.N ; i++ {
demo()
}
}

// time.Since 获取执行时间
start := time.Now()
http.Get(url)
time.Since(start)

//gin获取请求参数
1.表单请求
post form
header Content-Type application/x-www-form-urlencoded;
调用ParseForm后可用
//获取全部参数
context.Request.PostForm 
context.Request.Form
2.axios 正常请求
post body
header Content-Type application/json
//获取全部参数为json格式需处理
context.Request.Body
3.get
//获取全部参数
context.Request.URL.Query()

//调用结构方法
//obtain 结构体 path 方法 Call 传递的参数
respRaw := reflect.ValueOf(&obtain).MethodByName(method).Call([]reflect.Value{reflect.ValueOf(query)})
resp := respRaw[0].Interface()

//类型转换
strconv.Itoa("2") //整型转字符串
strconv.ParseInt("100", 10, 0) //字符串整型转