Go語言學習Tips(一)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

defer的邊界

defer是以函數為邊界的,也就是說,只有在當前函數將要退出的時候才會運行。


Nested channel select的返回


① 對於nested同一個channel select,如何想要層層返回,最好的方式就是直接close channel

package聽mainimport聽("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聽<-聽2close(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。


方法不是很完美,但是至少能比較優雅地解決本地開發編譯的問題。


本文出自:https://zhuanlan.zhihu.com/p/24436747?refer=auxten


■歡迎關注聽Reboot教育聽聽Python實戰班介紹---11月26日(周六)已開班

詳情點擊:http://www.51reboot.com/course/actual/

上課形式:面授班 / 網路直播班

報名QQ:979950755聽

本文出自 “Reboot營運開發” 部落格,請務必保留此出處http://opsdev.blog.51cto.com/2180875/1884242

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.