oinume journal

Scratchpad of what I learned

Go

OpenTelemetryとhttptrace.ClientTraceを使ってHTTPリクエストのlatencyを可視化する

Go

この記事は OpenCensusとhttptrace.ClientTraceを使ってHTTPリクエストのlatencyを可視化する - oinume journal のOpenTelemetry版。OpenTelemetryについては OpenTelemetryとは何か、そしてなぜそれが計装器の未来なのか? | New Relic を見てもらうのが手…

Go1.17beta1でGenericsに触れてみた

Go

Go1.17beta1がダウンロードできるようになったので、Generics(Type Parameters)でStackを書いて軽く遊んだメモ。 Generics (Type Parameters)について 最新の仕様のProposalは以下を見ると良い。 実際にどんな感じのコードになるのかは、GitHubのdev.typepar…

moq - gomockを使わないMock生成

Go

Goでよく使われるMockの生成ツールとしてgomockがある。個人的にはgomockが生成したコードでモックを書くのが好きではないので、代替としてmoqを使うやり方を取り上げてみようと思う。 TL;DR moqは、mockしたいinterfaceで定義されているメソッドと同じシグ…

go testを並列で動かして速くする

Go

はじめに アプリケーションが大きくなってくると、テストを並列で実行しないとどんどんgo testの実行時間が長くなってしまい、いわゆる「CI待ち」というものが発生してしまう。この記事は自分用のメモだが、テストを少しでも速くしたいという人のための記事…

OpenCensusとhttptrace.ClientTraceを使ってHTTPリクエストのlatencyを可視化する

Go

はじめに みなさんこんにちは。これはGo5 Advent Calendar 2019の19日目の記事です。この記事はOpenCensusとhttptrace.ClientTraceを使ってHTTPリクエストの内部的なlatencyを可視化する話です。「内部的なlatency」というのは、HTTPリクエストの中で名前解…

Go言語における並行処理 - ユースケース編

Go

はじめに これは以下の記事の続きの記事。以下の2つではgoroutineとchannelについて説明したので、これらを使って具体的な並行処理のユースケースを書いてみる。 Goにおける並行処理 - goroutine編 - oinume journal Goにおける並行処理 - channel編 - oinum…

Goにおける並行処理 - channel編

Go

はじめに これはGoにおける並行処理 - goroutine編 - oinume journalの続きの記事。goroutineに引き続き、Goの並行処理を支える重要な概念であるchannelについて説明する。 channelとは? channelはメモリに対するアクセスを同期するためやgoroutine間の通信…

Goにおける並行処理 - goroutine編

Go

はじめに Goでは、goroutineとchannelが言語仕様として組み込まれているため、他の言語に比べてとても並行処理のコードが書きやすい。この2つの基本的な動作原理についてまとめた自分用のメモである。(channelについては別の記事で書く予定) goroutineとは?…

Goのcontextによるキャンセルやタイムアウト

Go

これはなに? Go言語におけるcontextパッケージを使ったキャンセルやタイムアウトについて説明する。この記事を読むと以下について詳しくなれるはず...! context.WithCancel context.WithTimeout context.Done context.Err とはいいつつも、かなり自分向けの…

GCPのCloud PubSubをGoで動かしてみただけのメモ

これはGCPのCloud PubSubのチュートリアルをやってみただけの自分用のメモ。この記事で紹介されているサンプルコードはGitHubに置いてある。 Cloud PubSubとは GCPで提供されているメッセージキュー。メッセージの送信をして(publish)、複数のシステムがその…

クライアントを作って理解するOAuth2(実装編)

はじめに クライアントを作って理解するOAuth2(準備編) - oinume journalの続編。前の記事ではGoogle APIsのプロジェクトを作成してOAuth2 clientを登録した。この記事では発行されたClient idを使って実際にAccess tokenを取得する部分をGoで実装してみたい…

Satisfying a large interface quickly in Go

Sometimes it's painful to safisfy a large interface in Go. Here is a simple answer for this, just embed interface on struct like bellow: package main import ( "fmt" ) type Foo interface { MethodA() MethodB() MethodC() MethodD() } type FooI…

Goのhttpパッケージだけでmiddlewareを実装する

Go

標準ライブラリのhttpパッケージだけでもmiddlewareは簡単に作れますよ、というお話。 おさらい: http.Handlerまたはhttp.HandlerFuncでやり取りする Goのhttp.Handlerやhttp.HandlerFuncをちゃんと理解する - oinume journalに書いたとおり、 http.ListenAn…

GoのWeb Application Frameworkでオススメは?

Go

GoのWeb Application Frameworkでオススメは?という質問をよく受ける。 標準ライブラリのhttpパッケージ使えばおk とまずは返答している。ただ、RESTで様々なHTTPメソッドをサポートする必要があり、かつ /v1/users/{id} のようなPath内にIDが入る場合の対…

Goのhttp.Handlerやhttp.HandlerFuncをちゃんと理解する

Go

はじめに GoでHTTP Serverを作ろうとすると、標準ライブラリを使う場合以下のようなコードをよく書くと思う。 package main import ( "fmt" "log" "net/http" ) func main() { mux := http.NewServeMux() mux.Handle("/hello", http.HandlerFunc(hello)) log…

Generating an unpredictable random value in Go

There are a lot of examples to use math/rand. However, should use crypto/rand if you want to generate an unpredictable random value. That's because crypto/rand uses getrandom(2) if available, /dev/urandom otherwise on Linux. As a real worl…

Comparing datetime in Golang

Go

https://play.golang.org/p/5-BtaE6iXaP time.Before and time.After package main import ( "fmt" "time" ) func main() { t1 := time.Date(2018, 1, 1, 10, 0, 0, 0, time.UTC) t2 := time.Date(2018, 1, 1, 11, 0, 0, 0, time.UTC) fmt.Printf("t1.Before…

grpc-gatewayでRESTful APIを実装する

背景 何かしらの理由でRESTでAPIを実装しなくてはいけない時に、JSONを直接扱うのは面倒くさい。具体的には、JSONをデシリアライズして内部のデータ構造にマッピングする処理を書くのが面倒だ。というわけで、grpc-gatewayを使ってProtocol Buffersを定義す…

Handling uploaded files in Go

Go

Handling uploaded files in Go is quite easy. Here is a complete example. net/http.Request.ParseMultipartForm parses a request body as multipart/form-data. You can get files with net/http.Request.FormFile after calling the method net/http.R…

go test in practice

This blog post describes basics and practical examples of go test. Go’s automated test mechanism is well designed and easy to use. I’ll show you some techniques of go test in this article. What is go test go test is a command to run automa…

WebアプリケーションのE2EテストをGoで書く

Go

これはGo Advent Calendar 2016の18日目の記事です。今回はGoでE2Eテストを行うためのライブラリagoutiについて書きます。 GoでE2Eテストを書く理由 WebアプリケーションのサーバーサイドをGoで書いている場合、GoでE2Eテストを書くメリットとして JavaScrip…

Arguments to defered functions are evaluated when the defer executes

The arguments to the deferred function (which include the receiver if the function is a method) are evaluated when the defer executes, not when the call executes. Effective Go - The Go Programming Language I sometimes make a mistake that d…

Go Tip: Don't take the address of loop variable

stackoverflow.com If you take the address of loop variable, it may cause a bug which all values are same. You might expect following program prints 5 10 15 but it prints 15 15 15. That’s because loop variable v is initialized just once. pa…

What I read, watched, listened recently

dotGo 2014 - Andrew Gerrand - 5 things I love Andrew Gerrand is a Googler and worked for Go. I watched this video just to learn English but it was good that he was describing Go's feature very well. www.youtube.com Golang UK Conference 201…

Detecting duplicated code in Golang with CPD and Jenkins

Use CPD to detect duplicated code in Golang. And visualize how much duplicated by Jenkins DRY plugin.

Mocking a HTTP access with http.Transport in Golang

Mocking HTTP access with http.RoundTripper

Goで標準出力をキャプチャする

Go

GoLangで標準出力をキャプチャするやり方のコード例。

GoLangでJavaのenumっぽいライブラリ作った話

Go

GoLangでJavaのenumの機能を実装したらgoenumというライブラリを作ったよという話。

Macでファイルが変更されたら自動的にgofmtをかける

fswatchを使うと楽っぽい。 $ brew install fswatch $ fswatch . | xargs -n1 -I{} gofmt -w {} gofmt -w だと、stdoutに出力せずに直接対象ファイルに書き込まれる。

Goでnilなsliceやmapを返すと空のsliceやmapになる

Go

タイトルの通りだけど知らなかったので。明示的に空のsliceやmapを作らなくてもいいのは楽だ。 http://play.golang.org/p/veOWHGDdcn package main import "fmt" func emptyMap() map[string]string { return nil } func emptySlice() []string { return nil…