defer的边界
defer是以函数为边界的,也就是说,只有在当前函数将要退出的时候才会运行。
Nested channel select的返回
- 对于nested同一个channel select,如何想要层层返回,最好的方式就是直接close channel
package main
import (
"fmt"
"time"
)
func main() {
stopc := make(chan int)
go func() {
select {
case <-stopc:
fmt.Println("stopc 0")
select {
case <-stopc:
fmt.Println("stopc 1")
}
fmt.Println("stopc 2")
}
}()
// stopc <- 2
// stopc <- 2
close(stopc)
time.Sleep(time.Second * 5)
}
[Also SEE: Go Sandbox*
Fork别人项目的正确姿势
比如,有一个项目『http://github.com/sirupsen/logrus...』,你看他有点不爽想要改点东西。所以fork了一个新的项目『http://github.com/auxten/logrus...』,但代码里会有很多『import "http://github.com/sirupsen/logrus/xxx..."』
你直接去修改代码项目依旧会引用原版,pull request原作者又不能很快通过,急急急,这时候怎么办呢?
- go get别人代码,这里是
go get github.com/sirupsen/logrus
- 给项目直接添加一个新的remote,指向你自己的repo,命令:
git remote add auxten git@github.com:auxten/logrus.git
- 创建新branch,就叫做aux吧,并且让这个branch指向这个remote。命令:
git checkout -b aux && git branch -u auxten/master aux
- 最终的.git/config大概长这样:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/sirupsen/logrus
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "auxten"]
url = git@github.com:auxten/logrus.git
fetch = +refs/heads/*:refs/remotes/auxten/*
[branch "aux"]
remote = auxten
merge = refs/heads/master
[push]
default = upstream
- push代码就用
git push -u auxten aux:master
这条命令的意思就是:push并把aux的upstream设置成auxten的master。
方法不是很完美,但是至少能比较优雅地解决本地开发编译的问题。