<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Golang on 村边的池塘</title>
    <link>https://jerrywang1981.github.io/tags/golang/</link>
    <description>Recent content in Golang on 村边的池塘</description>
    <generator>Hugo</generator>
    <language>zh-CN</language>
    <lastBuildDate>Fri, 09 Apr 2021 20:40:45 +0800</lastBuildDate>
    <atom:link href="https://jerrywang1981.github.io/tags/golang/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Golang Oauth2 Github</title>
      <link>https://jerrywang1981.github.io/post/go/golang-oauth2-github/</link>
      <pubDate>Fri, 09 Apr 2021 20:40:45 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/golang-oauth2-github/</guid>
      <description>Github Oauth2 首先，在github上申请一个Oauth App, 获取client id / secret 需要这些信息，下边的代码是放在环境变量中 AUTH_URL = os.Getenv(&amp;#34;AUTH_URL&amp;#34;) // github authorization url TOKEN_URL = os.Getenv(&amp;#34;TOKEN_URL&amp;#34;) // github token url CLIENT_ID = os.Getenv(&amp;#34;CLIENT_ID&amp;#34;) // client id CLIENT_SECRET = os.Getenv(&amp;#34;CLIENT_SECRET&amp;#34;) // client secret REDIRECT_URL = os.Getenv(&amp;#34;REDIRECT_URL&amp;#34;) // the redirect url 在包的初始化代码中，初始化 var ( oauth2Config *oauth2.Config ) init() { oauth2Config = &amp;amp;oauth2.Config{ ClientID: CLIENT_ID, ClientSecret: CLIENT_SECRET, RedirectURL: REDIRECT_URL, // Discovery returns the OAuth2 endpoints. Endpoint: oauth2.Endpoint{ AuthURL: AUTH_URL, TokenURL: TOKEN_URL, AuthStyle: oauth2.AuthStyleInHeader, }, Scopes: []string{&amp;#34;openid&amp;#34;, &amp;#34;email&amp;#34;, &amp;#34;site_admin&amp;#34;, &amp;#34;repo&amp;#34;, &amp;#34;admin:org&amp;#34;, &amp;#34;user&amp;#34;}, } Login</description>
    </item>
    <item>
      <title>Terminate Golang App</title>
      <link>https://jerrywang1981.github.io/post/go/terminate-golang-app/</link>
      <pubDate>Sat, 06 Feb 2021 16:10:08 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/terminate-golang-app/</guid>
      <description>channel to terminate 在 main.go中, 创建一个channel接收os.Signal sigChan := make(chan os.Signal, 1) // ctrl+c-&amp;gt;SIGINT, kill -9 -&amp;gt; SIGKILL signal.Notify(sigChan, syscall.SIGINT, syscall.SIGKILL) 在main.go快结束的位置，加上 &amp;lt;-sigChan log.Info(&amp;#34;exit&amp;#34;) 如果用户有发送kill -9 或者ctrl+c， 会接收到这个信号。</description>
    </item>
    <item>
      <title>Golang Cron Jobs</title>
      <link>https://jerrywang1981.github.io/post/go/golang-cron-jobs/</link>
      <pubDate>Sat, 06 Feb 2021 16:06:53 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/golang-cron-jobs/</guid>
      <description>cron in golang 在golang中，有个包github.com/robfig/cron/v3可以实现linux中类似的cron功能，使用也比较简单。 官方文档中比较详细，这里只是简单记录一下。 c := cron.New() schlTime := &amp;#34;0 2 * * ?&amp;#34; // send reminder for org members c.AddFunc(schlTime, func() { log.Info(&amp;#34;Send reminder for org member expiration job started&amp;#34;) defer func() { log.Info(&amp;#34;Send reminder for org member expiration job finished&amp;#34;) }() log.Info(&amp;#34;started to remind expired org members&amp;#34;) err = task.RemindExpiredOrgMember(14) if err !=</description>
    </item>
    <item>
      <title>Gin Jwt Middleware</title>
      <link>https://jerrywang1981.github.io/post/go/gin-jwt-middleware/</link>
      <pubDate>Sat, 06 Feb 2021 14:56:43 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/gin-jwt-middleware/</guid>
      <description>JWT in golang golang中的jwt包一般是使用 github.com/dgrijalva/jwt-go, 而且godoc中已经有些example，这里只是记录一下使用 jwt.StandardClaims的情况。 func parseToken(tokenString string) (*jwt.StandardClaims, error) { token, err := jwt.ParseWithClaims(tokenString, &amp;amp;jwt.StandardClaims{}, func(t *jwt.Token) (interface{}, error) { return jwtKey, nil }) if err != nil { log.Errorf(&amp;#34;error: %v&amp;#34;, err) return nil, fmt.Errorf(&amp;#34;failed to parse the token string: %s&amp;#34;, tokenString) } log.Printf(&amp;#34;claims: %v&amp;#34;, token.Claims) if claims, ok := token.Claims.(*jwt.StandardClaims); ok &amp;amp;&amp;amp; token.Valid { return claims, nil } else { return nil, errors.Wrap(fmt.Errorf(&amp;#34;failed to valiate the token&amp;#34;), &amp;#34;failed&amp;#34;) } }</description>
    </item>
    <item>
      <title>Golang 中的零值</title>
      <link>https://jerrywang1981.github.io/post/go/zero-value-in-golang/</link>
      <pubDate>Tue, 18 Aug 2020 09:09:46 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/zero-value-in-golang/</guid>
      <description>zero value (零值) 在golang中，零值会在很多地方隐式的用到，所以把零值列出来。 0 所有的integer类型 (int32/int64/&amp;hellip;) 0.0 所有的float类型 (float32/float64/&amp;hellip;) false boolean类型 &amp;quot;&amp;quot; string类型， 这点要特别注意，从java过来的开发人员容易认为 nil是把 string和nil比较， 应该 str == &amp;quot;&amp;quot; ,不是 str == nil nil interface, slices,</description>
    </item>
    <item>
      <title>Golang Panic 的时候通过defer更新返回值</title>
      <link>https://jerrywang1981.github.io/post/go/golang-panic-recover-defer-update-return/</link>
      <pubDate>Mon, 17 Aug 2020 20:53:08 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/golang-panic-recover-defer-update-return/</guid>
      <description>在panic的函数中更新返回值 在golang中我们知道可以用defer, panic, recover来处理一些情况，比如，一般panic的话，如果在函数中没有处理，就会向上继续抛出 有时候，我们希望在出错的情况下，能够返回一个值。这个时候，defer就会比较好的处理这种情况。 func test() (r int) { defer func() { if</description>
    </item>
    <item>
      <title>Go Download From Private Repo in Docker</title>
      <link>https://jerrywang1981.github.io/post/go/go-download-from-private-repo-in-docker/</link>
      <pubDate>Fri, 24 Jul 2020 08:33:43 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/go-download-from-private-repo-in-docker/</guid>
      <description>问题 有时候开发的go project会用到一些私有仓库，或者自己搭的。比如用到了github.ibm.com里面的代码，如果在go.mod里面设置的 是github.ibm.com的网址的话，会发现build docker image的时候，提示access rights的问题。 方法 在你的Docke</description>
    </item>
    <item>
      <title>Go Gorm 操作数据库</title>
      <link>https://jerrywang1981.github.io/post/go/go-gorm/</link>
      <pubDate>Thu, 23 Jul 2020 08:25:59 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/go-gorm/</guid>
      <description>Go/Gorm 操作 Postgres 虽然go提供了database/sql包来操作数据库，但是如果有个ORM的包，可以直接操作model来操作数据库，就像node里面的sequelize,是不是 也是挺好的。Gorm就实现了这个功能，这个包目前是github上star数最多的go orm包，包括中文资料，对小伙</description>
    </item>
    <item>
      <title>Golang 导出 csv 乱码的问题</title>
      <link>https://jerrywang1981.github.io/post/go/golang-export-csv/</link>
      <pubDate>Tue, 12 May 2020 22:26:51 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/golang-export-csv/</guid>
      <description>问题提出 golang最近导出csv的时候，如果用excel打开，会有乱码。在网上查到了解决方案，记录一下。 解决方案 f, err := os.Create(&amp;#34;data.csv&amp;#34;) if err != nil { panic(err) } defer f.Close() f.WriteString(&amp;#34;\xEF\xBB\xBF&amp;#34;) // 写入UTF-8 BOM，避免使用Microsoft Excel打开乱码 writer := csv.NewWriter(f) writer.Write([]string{&amp;#34;col 1&amp;#34;, &amp;#34;col 2&amp;#34;, &amp;#34;col 3&amp;#34;}) writer.Flush()</description>
    </item>
    <item>
      <title>转换Pdf成图片</title>
      <link>https://jerrywang1981.github.io/post/go/convert-pdf-to-images/</link>
      <pubDate>Mon, 11 May 2020 21:51:52 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/convert-pdf-to-images/</guid>
      <description>背景 在做文字检测和文字识别的时候，有时候客户提供的是pdf格式的文件，而不是jpg/png格式，这时候就需要把pdf里面多个页面 保存成图片。 代码库pdf2image 实现 Python 参考readme docker 参考readme golang 参考readme 也可以 直接下载可执行文件直接执行 -&amp;gt; https://github.com/jerrywang1981/pdf2images/blob/master/pdf2image</description>
    </item>
    <item>
      <title>Golang 中通过gRPC调用Python实现的功能</title>
      <link>https://jerrywang1981.github.io/post/go/python-go-rpc/</link>
      <pubDate>Mon, 04 May 2020 00:11:12 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/python-go-rpc/</guid>
      <description>背景 有时候，我们在python中实现了一个功能，这功能如果用golang重新写呢，会比较麻烦，如果要在golang中调用python中的功能。 方式有很多，主要就是两个程序如果沟通的问题，那方式就各种各样了，可以通过http协议，json/xml等格式，或者tcp, 当然还有个 选择就</description>
    </item>
    <item>
      <title>Db2 Backup Tool</title>
      <link>https://jerrywang1981.github.io/post/go/db2-backup-tool/</link>
      <pubDate>Sat, 25 Apr 2020 20:28:35 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/db2-backup-tool/</guid>
      <description>为什么要做这个小工具 有时候，我们可能要把数据库一些表的数据导出，然后load到另外一个数据库，虽然有很多的工具可以选择， 但是可能用的不太顺手。所以就想，是不是能够自动生成export.sql文件。基本上来说，就是通过table，查询到 表结构，把所有的列选择出来，export到de</description>
    </item>
    <item>
      <title>02 原型模式</title>
      <link>https://jerrywang1981.github.io/post/go/02-prototype/</link>
      <pubDate>Sun, 12 Apr 2020 13:22:43 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/02-prototype/</guid>
      <description>原型模式 参考文章 原型模式 package prototype_pattern type Person struct { Name string Age int } type Cloner interface { Clone() interface{} } func (s *Person) Clone() interface{} { var n Person n.Name = s.Name n.Age = s.Age return n }</description>
    </item>
    <item>
      <title>01 单例模式</title>
      <link>https://jerrywang1981.github.io/post/go/01-singleton/</link>
      <pubDate>Sun, 12 Apr 2020 12:26:51 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/01-singleton/</guid>
      <description>单例模式 从网上找的 单例模式 golang实现 参考的文章Go语言中的单例模式 代码： 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package example import ( &amp;#34;sync&amp;#34; ) // the singleton struct/object type singleton struct{} var instance *singleton var once sync.Once func GetSingleInstance() *singleton { once.Do(func() { instance = &amp;amp;singleton{} }) return instance }</description>
    </item>
  </channel>
</rss>
