Go语言学习Tips(一)

Reboot_51 · · 704 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。


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原作者又不能很快通过,急急急,这时候怎么办呢?

  1. go get别人代码,这里是

    go get github.com/sirupsen/logrus
  2. 给项目直接添加一个新的remote,指向你自己的repo,命令:

    git remote add auxten git@github.com:auxten/logrus.git
  3. 创建新branch,就叫做aux吧,并且让这个branch指向这个remote。命令:

    git checkout -b aux && git branch -u auxten/master aux
  4. 最终的.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
  5. push代码就用

    git push -u auxten aux:master

这条命令的意思就是:push并把aux的upstream设置成auxten的master。

方法不是很完美,但是至少能比较优雅地解决本地开发编译的问题。


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:Reboot_51

查看原文:Go语言学习Tips(一)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

704 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传