Skip to content

Instantly share code, notes, and snippets.



View GitHub Profile
Last active Jul 26, 2021
【Golang】How to mock `os.Stdin` during the test in Go. 標準入力をモックする方法

「"golang" test "os.Stdin" "keinos"」でググってもヒットしなかったので、自分のググラビリティとして。

[Golang] How to mock os.Stdin during the test in Go

In the dependency injection point of view, it is a good practice to var OsStdin = os.Stdin and use OsStdin instead of os.Stdin.

But if the external package doesn't support that OsStdin alias feature we need to mock the os.Stdin.

Here's my snippet of the helper function to mock the os.Stdin.

Last active Jul 26, 2021
[Golang] How to replace all whitespaces into a single space without using regex.

[Golang] How To Replace All Whitespaces Into a Single Space in Go

Trimming/replacing repeated whitespaces such as space, tab, line-breaks, etc as one space.

Something like " foo\n\t\t bar\n buzz" to "foo bar buzz". But without using regex nor multiple strings.Replace().


Use strings.Fiels() and join them.

Last active Jun 24, 2021
[Golang][BadgerDB] How to suppress/hide error messages

How To Suppress/Hide Error/INFO Messages In Badger DB


Set Logger field of badger.Options object to nil

pathDirDB := GetMyPathDirDB()
optionsDB := badger.DefaultOptions(pathDirDB)
if !IsModeDebug {
Last active Mar 28, 2021
[Golang][go-carpet] Bash sample of how to display / print only uncovered (not 100% of coverage) source files in "go-carpet" of Golang.
# getNameFilesUncovered echoes file names in comma-separated list which didn't
# cover 100%. This will be used for “go-carpet` -file option.
function getNameFilesUncovered() {
list=$(go tool cover -func="$name_file_coverage" | grep -v '100.0%\|init\|total' | awk '{print $1}')
echo "$list" | while IFS= read -r line; do
basename "${line}" | sed -e 's/\.[^\.]*$/\.go/'
KEINOS / docker-compose.yml
Last active Aug 15, 2020
Docker-compose file for WordPress 5.5 + PHP 7.4.9, Apache 2.4.38 on the fly.
View docker-compose.yml
# docker-compose.yml for WordPress 5.5 + PHP 7.4.9, Apache 2.4.38
# - Download URL:
# - How to run servers:
# $ php -r 'copy("", "./docker-compose.yml");'
# $ docker-compose up -d
# - How to access:
# - Open browser: http://localhost:8989/
# - Ref: @ Youtube
version: '3'
KEINOS / 0.PregMatchVsCtypeAlnumBench.php
Last active May 19, 2020
[Bench] Which is faster, preg_match() or ctype_alnum() ?
View 0.PregMatchVsCtypeAlnumBench.php
* Benchmark between preg_match() and ctype_alnum()
* $ phpbench run --retry-threshold=1 --revs=1000000 \
* --iterations=10 --report=compare --report=aggregate \
* ./PregMatchVsCtypeAlnumBench.php
* Requirement
* $ composer require phpbench/phpbench @dev --dev
View Main.php
* Sample PHP script to check if a string is available to use in namespace.
* View this script in action online:
* View this script in Gist:
Created Jul 6, 2019
[LOG] Error log on heroku
2019-07-06T16:11:37.070517+00:00 app[web.1]: 2019-07-07 01:11:37: (network.c.313) can't bind to socket: Permission denied
2019-07-06T16:11:38.074704+00:00 app[web.1]: 2019-07-07 01:11:38: (network.c.313) can't bind to socket: Permission denied
2019-07-06T16:11:39.079038+00:00 app[web.1]: 2019-07-07 01:11:39: (network.c.313) can't bind to socket: Permission denied
2019-07-06T16:11:40.083423+00:00 app[web.1]: 2019-07-07 01:11:40: (network.c.313) can't bind to socket: Permission denied
2019-07-06T16:11:41.087808+00:00 app[web.1]: 2019-07-07 01:11:41: (network.c.313) can't bind to socket: Permission denied
2019-07-06T16:11:42.092604+00:00 app[web.1]: 2019-07-07 01:11:42: (network.c.313) can't bind to socket: Permission denied
2019-07-06T16:11:43.097222+00:00 app[web.1]: 2019-07-07 01:11:43: (network.c.313) can't bind to socket: Permission denied
2019-07-06T16:11:44.101447+00:00 app[web.1]: 2019-07-07 01:11:44: (network.c.313) can'
KEINOS / dockle-status.json
Last active Jun 21, 2019
Sample JSON of Shields.IO status badge
View dockle-status.json
"schemaVersion": 1,
"label": "Dockle",
"message": "FATAL 1 WARN 2",
"color": "red"
$ # Env infos
$ # ---------
$ docker version
Client: Docker Engine - Community
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:47:43 2018
OS/Arch: darwin/amd64