go
张先生的记录本
仰望星空,日积月累。
展开
-
go_gorm_join
var res []*Result db.Table("student").Select("student.name,student.class_id,student.sex,student.score,class.class_name"). Joins("left join class on class.class_id = student.class_id").Scan(&res) fmt.Println(*res[0]) res := &[]Result{} db.T.原创 2021-06-26 21:45:27 · 3537 阅读 · 0 评论 -
go传输csv格式
"github.com/gocarina/gocsv" content, err := gocsv.MarshalString(&result) common.CsvDownload(ctx, "列表导出", content)func CsvDownload(ctx iris.Context, filename, content string) { ctx.Header("Content-Type", "application/csv") ctx.Header("Co...原创 2021-02-19 17:12:22 · 344 阅读 · 0 评论 -
go 锁
互斥锁互斥锁控制下的多个写操作之间、多个读操作之间是互斥的,并且写操作与读操作之间也是互斥的。互斥锁适用于读写不确定场景,即读写次数没有明显的区别,并且只允许只有一个读或者写的场景,所以该锁叶叫做全局锁,也就是完全互斥。var mutex sync.Mutex func write() {mutex.Lock() //加互斥锁defer mutex.Unlock() //解互斥锁// 省略若干条语句}读写锁读写锁控制下的多个写操作之间都是互斥的,并且写操作与读操作之间也都是互斥的。但原创 2021-01-03 20:41:53 · 180 阅读 · 0 评论 -
go_goroutine
协程创建使用 go 关键字创建 goroutine 时,被调用函数的返回值会被忽略。如果需要在 goroutine 中返回数据,需要通道(channel)把数据从 goroutine中作为返回值传出go 函数名( 参数列表 )优点:开销小每个协程goroutine 默认占用的内存仅2Kb(线程8Mb),远比java和c少调度性能好golang可以控制协程goroutine的调度,使得在一个合适的时间进行GC通道(channel)一个 channels 是一个通信机制,它可以让原创 2021-01-03 12:32:35 · 136 阅读 · 0 评论 -
go异常/错误机制(panic和error)
deferdefer是go提供的一种延迟执行机制。后进先出原则当执行defer时,会将对应的函数压入栈中,在函数返回或者panic异常结束时,go会从栈中取出延迟函数执行。func main(){ defer fmt.Println("1") defer fmt.Println("2") defer fmt.Println("3") fmt.Println("hello")}打印结果为hello321立即解析原则 i := 1 defer fmt.Printf("fir转载 2021-01-03 11:35:42 · 1293 阅读 · 0 评论 -
go_error
go的错误表示方法转载自链接error简单表示方法package errors// New returns an error that formats as the given text.func New(text string) error { return &errorString{text}}// errorString is a trivial implementation of error.type errorString struct { s string}转载 2021-01-02 21:36:06 · 80 阅读 · 0 评论 -
go解析json
这里写目录标题json标签生成json解析jsonjson标签type Author struct { Name string `json:"name,omitempty"` Age int32 `json:"age,string,omitempty"`}常见的两个标签是 string 和 omitempty,前者表示当前的整数或者浮点数是由 JSON 中的字符串表示的,而另一个字段 omitempty 会在字段为零值时,直接在生成的 JSON 中忽略对应的键值对,例如:“ag原创 2021-01-02 21:30:40 · 184 阅读 · 1 评论 -
go语言与开发
这里写目录标题go基础go基础package mainimport ( "fmt" "time")func max(num1, num2 int) int { var res int if(num1 > num2){ res = num1 } else { res = num2 } return res}type books struct { title string author string bookId int time time.Time}//原创 2020-11-10 09:51:34 · 76 阅读 · 0 评论