Skip to content

Instantly share code, notes, and snippets.

View akm's full-sized avatar

Takeshi Akima akm

View GitHub Profile
@akm
akm / friendsofgo-errors-main.go
Last active November 23, 2023 13:07
Stacktrace examples
package main
import (
"fmt"
"github.com/friendsofgo/errors"
)
func main() {
err := foo1()
@akm
akm / error_recovery_in_defered_function.go
Created October 11, 2023 07:52
Error recovery in deferred function
// Use go playground or https://github.com/traefik/yaegi to try
func foo(v int) (rerr error) {
if v%3 == 1 {
defer func() {
rerr = fmt.Errorf("error %d mod 3 => 1: overwrite", v)
}()
} else if v%3 == 2 {
defer func() {
err := fmt.Errorf("error %d mod 3 => 2: set error if nil", v)
@akm
akm / .golangci.yaml
Created January 18, 2023 23:47
My first golangci-lint
# https://golangci-lint.run/usage/configuration/
run:
timeout: 5m
linters-settings:
govet:
settings:
unusedresult:
funcs:
- github.com/akm/repo1/path/to/package1.Function1
@akm
akm / exercise-web-crawler.go
Created August 17, 2022 01:55
Web Crawler: Concurrency - A tour of Go
package main
// This is my first go program written in Dec 22nd, 2016
import (
"fmt"
"sync"
)
type Fetcher interface {
ARGV.each do |keyword|
image_family_names = `docker search #{keyword} --format '{{.Name}}'`.split("\n")
puts( (["image family names:"] + image_family_names).join("\n") )
image_family_names.each do |image_family_name|
tags = `curl -s https://registry.hub.docker.com/v1/repositories/#{image_family_name}/tags | jq -r '.[].name'`.split("\n")
puts("#{image_family_name} tags: #{tags.join(',')}")
tags.each do |tag|
image_name = image_family_name + ':' + tag
file_name = image_family_name.gsub('/', '-') + '-' + tag + '.tar.gz'
if File.exist?(file_name)
@akm
akm / prepend_example.rb
Last active June 20, 2020 02:00
prependでモジュールを拡張する例
module Bar
def bar
"bar"
end
end
class Foo
include Bar
end
class Prcss
attr_accessor :pid, :ppid, :command
attr_reader :done
def initialize(pid, ppid, command)
@pid = pid.to_i
@ppid = ppid.to_i
@command = command
end
require 'benchmark'
n = 50_000
Benchmark.bm do |x|
x.report { n.times{ I18n.t('controller.acquisitions.cancel_execution') } }
x.report { n.times{ I18n.t(:cancel_execution, scope: 'controller.acquisitions') } }
x.report { n.times{ I18n.t(:cancel_execution, scope: "controller.acquisitions") } }
x.report { n.times{ I18n.t(:cancel_execution, scope: [:controller, :acquisitions]) } }
end
# Usage:
# $ GCP_PROJECT=... ruby update-fwrules4appsscript.rb
#
# See https://cloud.google.com/appengine/kb/#static-ip
APP_ENGINE_IP_RANGE = (8000...9000)
def run(cmd)
puts cmd
raise "Failed to #{cmd}" unless system(cmd)
end
# Summarize JSON structure
# cat source.json | ruby sum_json.rb --key-pattern '\A\-|\-test\z' --format json
require 'json'
require 'yaml'
require 'getoptlong.rb'
parser = GetoptLong.new
parser.set_options(
['--key-pattern', '-k', GetoptLong::OPTIONAL_ARGUMENT],