<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Go on 日日是好日-我的生活记录</title>
    <link>http://www.ririshihaori.com:80/categories/go/</link>
    <description>Recent content in Go on 日日是好日-我的生活记录</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Wed, 28 Apr 2021 11:26:26 +0800</lastBuildDate>
    <atom:link href="http://www.ririshihaori.com:80/categories/go/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>第一次提交golangci-lint报错</title>
      <link>http://www.ririshihaori.com:80/posts/2021/4/go_lint_errors/</link>
      <pubDate>Wed, 28 Apr 2021 11:26:26 +0800</pubDate>
      <guid>http://www.ririshihaori.com:80/posts/2021/4/go_lint_errors/</guid>
      <description>&lt;p&gt;　　第一次开发的分支代码直接提交到远程后，触发了gitlab的pipeline，先是报了lint的几个错误，&#xA;修改提交之后又报了unittest的错误。&lt;!-- raw HTML omitted --&gt;&#xA;　　之后在把本地分支push到远程分支之前可以先本地&lt;code&gt;golangci-lint run ./... &lt;/code&gt;过lint，再&lt;code&gt;go test -v -cover ./cmd/...&lt;/code&gt;过单元测试再提交。&lt;/p&gt;&#xA;&lt;!-- raw HTML omitted --&gt;&#xA;&lt;p&gt;　　这里报的lint的错误是&lt;code&gt;Implicit memory aliasing in for loop&lt;/code&gt;，刚开始以为是在for循环里的函数调用传地址会报错，后来根据https://stackoverflow.com/questions/62446118/implicit-memory-aliasing-in-for-loop 里的说法，发现是因为for _, x range y的写法,循环里如果有传地址的操作，其实传的一直是x的地址，据说多个协程的场景下可能会有风险。&lt;!-- raw HTML omitted --&gt;&#xA;　　修改的方式很简单，把for循环的写法改成使用index遍历的方式即可，或者把函数实现从传指针改成传值的方式也可以。&lt;/p&gt;</description>
    </item>
    <item>
      <title>Go语言学习资料汇总</title>
      <link>http://www.ririshihaori.com:80/posts/2021/4/go_learning/</link>
      <pubDate>Fri, 02 Apr 2021 14:37:10 +0800</pubDate>
      <guid>http://www.ririshihaori.com:80/posts/2021/4/go_learning/</guid>
      <description>&lt;p&gt;　　GO语言学习过程中遇到的一些比较好的资料：&lt;/p&gt;&#xA;&lt;h3 id=&#34;博文&#34;&gt;博文&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://lessisbetter.site/2019/06/09/golang-first-class-function&#34;&gt;First class function in Go&lt;/a&gt;&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;讲了go语言中函数如何作为一等公民&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://segmentfault.com/a/1190000017958702&#34;&gt;总结了才知道，原来channel有这么多用法！&lt;/a&gt;&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;讲channel定义和用法，有一些笔误，参考原文评论区&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://sanyuesha.com/2017/07/22/how-to-understand-go-interface/&#34;&gt;理解 Go interface 的 5 个关键点&lt;/a&gt;&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;有一点难，例子有点抽象&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://studygolang.com/articles/10167&#34;&gt;golang中defer的使用规则&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;学习站点&#34;&gt;学习站点&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://gobyexample.com/&#34;&gt;Go by Example&lt;/a&gt;（很短 有例子比较好理解）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;http://go-database-sql.org/&#34;&gt;Go database/sql tutorial&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://books.studygolang.com/gopl-zh/&#34;&gt;Go 语言圣经&lt;/a&gt;（有比较具体的实例）&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;书籍&#34;&gt;书籍&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/tiancaiamao/go-internals&#34;&gt;《深入解析Go》&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/astaxie/Go-in-Action&#34;&gt;《Go实战开发》&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/Unknwon/the-way-to-go_ZH_CN&#34;&gt;《Go入门指南》&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/polaris1119/The-Golang-Standard-Library-by-Example&#34;&gt;《Go语言标准库》&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/astaxie/build-web-application-with-golang&#34;&gt;《Go Web 编程》&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/achun/Go-Blog-In-Action&#34;&gt;《Go语言博客实践》&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/qyuhen/book&#34;&gt;《Go语言学习笔记》&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;翻译&#34;&gt;翻译&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://golang.org/doc/effective_go.html&#34;&gt;Effective Go&lt;/a&gt; 英文版&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/Unknwon/the-way-to-go_ZH_CN&#34;&gt;The Way to Go&lt;/a&gt; 中文版&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/miekg/gobook&#34;&gt;《Learning Go》&lt;/a&gt;英文版:=&amp;gt;&lt;a href=&#34;https://github.com/mikespook/Learning-Go-zh-cn&#34;&gt;《Learning Go》&lt;/a&gt; 中文版&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;教程&#34;&gt;教程&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/Unknwon/go-fundamental-programming&#34;&gt;《Go编程基础》&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/Unknwon/go-web-foundation&#34;&gt;《Go Web基础》&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/Unknwon/go-rock-libraries-showcases&#34;&gt;《Go名库讲解》&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/hyper-carrot/go_command_tutorial&#34;&gt;Go 命令教程&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;</description>
    </item>
    <item>
      <title>Go实现String Set</title>
      <link>http://www.ririshihaori.com:80/posts/2020/4/go_sets/</link>
      <pubDate>Wed, 01 Apr 2020 01:13:28 +0800</pubDate>
      <guid>http://www.ririshihaori.com:80/posts/2020/4/go_sets/</guid>
      <description>&lt;p&gt;基于map实现sets.go&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;package sets&#xA;&#xA;// An Empty struct does not take up any memory&#xA;type Empty struct{}&#xA;&#xA;// sets.String is a set of strings, implemented via map[string]struct{} for minimal memory consumption.&#xA;type String map[string]Empty&#xA;&#xA;&#xA;func NewString(items ...string) String {&#xA;&#x9;s := String{}&#xA;&#x9;s.Insert(items...)&#xA;&#x9;return s&#xA;}&#xA;&#xA;func (s String) Insert(items ...string) {&#xA;&#x9;for _, item := range items {&#xA;&#x9;&#x9;s[item] = Empty{}&#xA;&#x9;}&#xA;}&#xA;&#xA;func (s String) Delete(items ...string) {&#xA;&#x9;for _, item := range items {&#xA;&#x9;&#x9;delete(s, item)&#xA;&#x9;}&#xA;}&#xA;&#xA;func (s String) Has(item string) bool {&#xA;&#x9;_, res := s[item]&#xA;&#x9;return res&#xA;}&#xA;&#xA;// HasAll returns true if and only if all items are contained in the set.&#xA;func (s String) HasAll(items ...string) bool {&#xA;&#x9;for _, item := range items {&#xA;&#x9;&#x9;if !s.Has(item) {&#xA;&#x9;&#x9;&#x9;return false&#xA;&#x9;&#x9;}&#xA;&#x9;}&#xA;&#x9;return true&#xA;}&#xA;&#xA;// HasAny returns true if any items are contained in the set.&#xA;func (s String) HasAny(items ...string) bool {&#xA;&#x9;for _, item := range items {&#xA;&#x9;&#x9;if s.Has(item) {&#xA;&#x9;&#x9;&#x9;return true&#xA;&#x9;&#x9;}&#xA;&#x9;}&#xA;&#x9;return false&#xA;}&#xA;&#xA;// Difference returns a set of objects that are not in s2&#xA;func (s String) Difference(s2 String) String {&#xA;&#x9;res := NewString()&#xA;&#x9;for item := range s {&#xA;&#x9;&#x9;if !s2.Has(item) {&#xA;&#x9;&#x9;&#x9;res.Insert(item)&#xA;&#x9;&#x9;}&#xA;&#x9;}&#xA;&#x9;return res&#xA;}&#xA;&#xA;// Union returns a new set which includes items in either s1 or s2.&#xA;func (s1 String) Union(s2 String) String {&#xA;&#x9;res := NewString()&#xA;&#x9;for item := range s1 {&#xA;&#x9;&#x9;res.Insert(item)&#xA;&#x9;}&#xA;&#x9;for item := range s2 {&#xA;&#x9;&#x9;res.Insert(item)&#xA;&#x9;}&#xA;&#x9;return res&#xA;}&#xA;&#xA;// Intersection returns a new set which includes the item in BOTH s1 and s2&#xA;func (s1 String) Intersection(s2 String) String {&#xA;&#x9;res := NewString()&#xA;&#x9;if s1.Len() &amp;lt; s2.Len() {&#xA;&#x9;&#x9;for item := range s1 {&#xA;&#x9;&#x9;&#x9;if s2.Has(item) {&#xA;&#x9;&#x9;&#x9;&#x9;res.Insert(item)&#xA;&#x9;&#x9;&#x9;}&#xA;&#x9;&#x9;}&#xA;&#x9;} else {&#xA;&#x9;&#x9;for item := range s2 {&#xA;&#x9;&#x9;&#x9;if s1.Has(item) {&#xA;&#x9;&#x9;&#x9;&#x9;res.Insert(item)&#xA;&#x9;&#x9;&#x9;}&#xA;&#x9;&#x9;}&#xA;&#x9;}&#xA;&#x9;return res&#xA;}&#xA;&#xA;func (s String) Len() int {&#xA;&#x9;return len(s)&#xA;}&#xA;&#xA;func (s String) List() []string {&#xA;&#x9;res := make([]string, 0, len(s))&#xA;&#x9;for item := range s {&#xA;&#x9;&#x9;res = append(res, item)&#xA;    }&#xA;&#x9;return res&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;单元测试 sets_test.go&lt;/p&gt;</description>
    </item>
    <item>
      <title>[转载]Golang新手可能会踩的50个坑</title>
      <link>http://www.ririshihaori.com:80/posts/2020/3/go_traps_and_common_mistakes/</link>
      <pubDate>Sun, 29 Mar 2020 19:26:30 +0800</pubDate>
      <guid>http://www.ririshihaori.com:80/posts/2020/3/go_traps_and_common_mistakes/</guid>
      <description>&lt;p&gt;使用Golang时读到了这篇文章，感觉每一条都非常有用，所以转到这里。感谢原文作者和译者。&lt;/p&gt;&#xA;&lt;p&gt;译文地址：https://segmentfault.com/a/1190000013739000#articleHeader43     &lt;br&gt;&#xA;原文地址：http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/&lt;/p&gt;&#xA;&lt;h2 id=&#34;前言&#34;&gt;前言&lt;/h2&gt;&#xA;&lt;p&gt;Go 是一门简单有趣的编程语言，与其他语言一样，在使用时不免会遇到很多坑，不过它们大多不是 Go 本身的设计缺陷。如果你刚从其他语言转到 Go，那这篇文章里的坑多半会踩到。&lt;/p&gt;&#xA;&lt;p&gt;如果花时间学习官方 doc、wiki、&lt;a href=&#34;https://groups.google.com/forum/#!forum/golang-nuts&#34;&gt;讨论邮件列表&lt;/a&gt;、 &lt;a href=&#34;https://github.com/robpike&#34;&gt;Rob Pike&lt;/a&gt; 的大量文章以及 Go 的源码，会发现这篇文章中的坑是很常见的，新手跳过这些坑，能减少大量调试代码的时间。&lt;/p&gt;&#xA;&lt;h3 id=&#34;1-左大括号--一般不能单独放一行&#34;&gt;1. 左大括号 &lt;code&gt;{&lt;/code&gt; 一般不能单独放一行&lt;/h3&gt;&#xA;&lt;p&gt;在其他大多数语言中，&lt;code&gt;{&lt;/code&gt; 的位置你自行决定。Go 比较特别，遵守分号注入规则（automatic semicolon injection）：编译器会在每行代码尾部特定分隔符后加 &lt;code&gt;;&lt;/code&gt; 来分隔多条语句，比如会在 &lt;code&gt;)&lt;/code&gt; 后加分号：&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// 错误示例&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;func&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;()&#x9;&#x9;&#x9;&#x9;&#x9;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;println(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;hello world&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// 等效于&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;func&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;();&#x9;&lt;span style=&#34;color:#75715e&#34;&gt;// 无函数体&#x9;&#x9;&#x9;&#x9;&#x9;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;println(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;hello world&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;&amp;gt; ./main.go: missing function body             &#xA;&amp;gt; ./main.go: syntax error: unexpected semicolon or newline before {&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// 正确示例&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;func&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;() {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;println(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;hello world&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}     &#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;注意代码块等特殊情况：&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
