概要 Goにはsingleflightという重複した関数呼び出しを抑制する仕組みがあります。 例えばキャッシュが切れた時にOriginへのアクセスが並行して走ってしまう(Cache Stampede)ケースでサーバやDBがスローダウンする問題がありますが、こういった時にsingleflightを挟むと解決できます。 環境 golang 1.12.5 簡単なサンプル 通常だと10回呼び出す処理を、singleflightで抑制してみます。 実装 package main import ( "fmt" "log" "sync" "time" "golang.org/x/sync/singleflight" ) var group singleflight.Group func doSomething(name string) { v, err, shared := group.Do(name,
はじめにGo1.22リリース連載 の5本目です。 本記事ではGoの標準ライブラリである net/http の ServeMux におけるルーティング周りの強化について取り上げます。 関連する Release Note と Issue はこちらを参照してください。 https://tip.golang.org/doc/go1.22#enhanced_routing_patterns https://github.com/golang/go/issues/61410 変更点HTTPメソッドの指定が可能にServeMux.Handle や ServeMux.HandleFunc を使用してハンドラを登録する際に GET /xxx のようにHTTPメソッド指定して、ハンドラを呼び分けることができるようになりました。 mux := http.NewServeMux() // GETを指定したハンドラ
A common question developers new to Go have is “How do I organize my Go project?”, in terms of the layout of files and folders. The goal of this document is to provide some guidelines that will help answer this question. To make the most of this document, make sure you’re familiar with the basics of Go modules by reading the tutorial and managing module source. Go projects can include packages, co
はじめに 2023年8月9日(日本の場合)Go1.21がリリースされました🎉。Go1.21ではさまざまな変更点や追加機能が加わります。その中でもGo標準ライブラリに導入される構造化ロギングパッケージlog/slog(以下、slog)を楽しみにしている方は多いのではないでしょうか? 本稿では、slogを実際に触りつつ、機能の解説をしていきます。 TL;DR 本稿の概要をスライドにもまとめているので、ご参考にして下さい。 従来のlogパッケージについて slogの説明に入る前に、Go標準のlogパッケージについて簡単に紹介する。logパッケージを用いると、 io.Writer インターフェースを実装する任意の型にログメッセージを書き込むことができる。しかし以下のような制限があった。 ログレベルをサポートしていない ログレベルはほとんどのログパッケージの定番機能の一つだが、logパッケージには
※この投稿は米国時間 2023 年 8 月 2 日に、Google Cloud blog に投稿されたものの抄訳です。 Go はクラウドベースの開発のために世界的に採用されている主要なプログラミング言語です。クラウド アプリケーションやビジネス クリティカルなクラウド インフラストラクチャを構築、スケーリングする目的で何百万人もの開発者に利用されています。CLI、ウェブ アプリケーション、クラウド サービス、ネットワーク サービスなど、どのようなものを構築するにしても、Go は習得するのも保守も容易で、組み込みの同時実行性や堅牢な標準ライブラリをはじめとする便利な機能が満載です。 このたび、Go を Google Cloud で使い始める際のハードルを少し下げることが可能になります。Go は最近、事前定義されたテンプレートを使用して新しいプロジェクトを Go でインスタンス化できる、gon
Eli Bendersky, on behalf of the Go team 8 August 2023 Today the Go team is thrilled to release Go 1.21, which you can get by visiting the download page. Go 1.21 is packed with new features and improvements. Here are some of the notable changes; for the full list, refer to the release notes. Tool improvements The Profile Guided Optimization (PGO) feature we announced for preview in 1.20 is now gene
Introduction to Go 1.20 The latest Go release, version 1.20, arrives six months after Go 1.19. Most of its changes are in the implementation of the toolchain, runtime, and libraries. As always, the release maintains the Go 1 promise of compatibility. We expect almost all Go programs to continue to compile and run as before. Changes to the language Go 1.20 includes four changes to the language. Go
errors, fmt: add support for wrapping multiple errors · golang/go@4a0a2b3 · GitHub An error which implements an "Unwrap() []error" method wraps all the non-nil errors in the returned ... https://github.com/golang/go/commit/4a0a2b33dfa3c99250efa222439f2c27d6780e4a Go でエラーを扱う際に、複数のエラーを束ねたい事があります。例えば複数のタスクを実行し、1つでもエラーになれば中断するのではなく、一通りタスクを実施し終えた結果を返したい様なニーズです。 package main import ( "errors" "log" "os"
Introduction to Go 1.19 The latest Go release, version 1.19, arrives five months after Go 1.18. Most of its changes are in the implementation of the toolchain, runtime, and libraries. As always, the release maintains the Go 1 promise of compatibility. We expect almost all Go programs to continue to compile and run as before. Changes to the language There is only one small change to the language, a
2023.10.5追記: Goチームからプロジェクトの目的に応じたディレクトリ構造についてのドキュメントが公式に公開されています。 https://go.dev/doc/modules/layout Goでプロジェクトのフォルダ構成どうしよう、とググると見つかるStandard Go Project Layout。とはいえ、これはかなりコード量を増やしてしまう恐れがありますので、導入する場合のデメリットも考えておく方が良いです。 特に、プログラマーは、最初にみたプログラミング言語のフォルダ構成を親だと思う特性があり、Javaや.NETに影響されるとかなり細かくフォルダを切りたくなったり、package privateなど細かく可視性を制御しようとしたりして、なおかつ「privateのテストってどうすべきなんですか?」とか議論を始めたりもしますが、Go先生によればこれぐらいは1パッケージにフ
Standard Go Project Layout Translations: 한국어 문서 简体中文 正體中文 简体中文 - ??? Français 日本語 Português Español Română Русский Türkçe Italiano Vietnamese Українська Indonesian Overview This is a basic layout for Go application projects. It's not an official standard defined by the core Go dev team; however, it is a set of common historical and emerging project layout patterns in the Go ecosystem. Some of thes
✨ samber/lo is a Lodash-style Go library based on Go 1.18+ Generics. This project started as an experiment with the new generics implementation. It may look like Lodash in some aspects. I used to code with the fantastic "go-funk" package, but "go-funk" uses reflection and therefore is not typesafe. As expected, benchmarks demonstrate that generics are much faster than implementations based on the
github.com/Code-Hex/dd という Go の構文として有効なフォーマットで pretty print を行えるライブラリを作成しました。(Star をくれると大喜びします) 色付き pretty print をするとこんな感じになります。 作った背景 pretty print 可能なライブラリは既にいくつか存在します。有名どころだと github.com/davecgh/go-spew/spew や github.com/k0kubun/pp/v3 があります。私はこれらのライブラリがとても好きで良く利用しています。パッケージ名は k0kubun/pp を真似しています。こちらは data dumper の略で dd です。 しかし、JSON や YAML などのデータを Go の構造体へマッピングし、その結果をテストデータとして比較したい時に、上記で紹介したライブラリの
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く