参考书:Go语言编程
第七章 工程管理
1 go version 命令查看版本号
2 Go语言明确拥护骆驼命名法
3 go help xxx 命令查看xxx的信息
4 go fmt xxx.go 格式化xxx.go文件内容
第九章 进阶话题
1 反射是把双刃剑,功能强大但代码可读性并不理想。若非必要,并不推荐使用反射
2 获取类型信息
import (
"fmt" "reflect" ) func main() { var x float64 = 3.4 fmt.Println("type:", reflect.TypeOf(x)) v := reflect.ValueOf(x) fmt.Println("type:", v.Type()) fmt.Println("kind is float64:", v.Kind() == reflect.Float64) fmt.Println("value:", v.Float()) }
输出type: float64
type: float64
kind is float64: true
value: 3.4
3 获取类型值
var x float64 = 3.4 p := reflect.ValueOf(&x) // 注意:得到X的地址 fmt.Println("type of p:", p.Type()) fmt.Println("settability of p:" , p.CanSet()) v := p.Elem() fmt.Println("settability of v:" , v.CanSet()) v.SetFloat(7.1) fmt.Println(v.Interface()) fmt.Println(x)
输出
type of p: *float64
settability of p: false
settability of v: true
7.1
7.1
4 对结构的反射操作
type T struct { A int B string } t := T{203, "mh203"} s := reflect.ValueOf(&t).Elem() typeOfT := s.Type() for i := 0; i < s.NumField(); i++ { f := s.Field(i) fmt.Printf("%d: %s %s = %v\n", i,typeOfT.Field(i).Name, f.Type(), f.Interface()) }
输出
0: A int = 203
1: B string = mh203
5 协程,也被称为轻量级线程,有如下几个特点
能够在单一的系统线程中模拟多个任务的并发执行。
在一个特定的时间,只有一个任务在运行,即并非真正地并行。
被动的任务调度方式,即任务没有主动抢占时间片的说法。当一个任务正在执行时,外部没有办法中止它。要进行任务切换,只能通过由该任务自身调用yield()来主动出让CPU使用权。
每个协程都有自己的堆栈和局部变量。