Skip to content

Instantly share code, notes, and snippets.

Avatar

KEINOS KEINOS

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

「"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.

@KEINOS
KEINOS / replace_whitespaces.md
Last active Jul 26, 2021
[Golang] How to replace all whitespaces into a single space without using regex.
View replace_whitespaces.md

[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().

TL; DR

Use strings.Fiels() and join them.

@KEINOS
KEINOS / how-to-hide-info-msg-in-badger.md
Last active Jun 24, 2021
[Golang][BadgerDB] How to suppress/hide error messages
View how-to-hide-info-msg-in-badger.md

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

TL; DR

Set Logger field of badger.Options object to nil

pathDirDB := GetMyPathDirDB()
optionsDB := badger.DefaultOptions(pathDirDB)
if !IsModeDebug {
@KEINOS
KEINOS / sample.sh
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.
View sample.sh
#!/bin/bash
name_file_coverage='coverage.out'
# 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
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: https://git.io/JJ5Vu
# - How to run servers:
# $ php -r 'copy("https://git.io/JJ5Vu", "./docker-compose.yml");'
# $ docker-compose up -d
# - How to access:
# - Open browser: http://localhost:8989/
# - Ref: https://www.youtube.com/watch?v=pYhLEV-sRpY @ Youtube
version: '3'
@KEINOS
KEINOS / 0.PregMatchVsCtypeAlnumBench.php
Last active May 19, 2020
[Bench] Which is faster, preg_match() or ctype_alnum() ?
View 0.PregMatchVsCtypeAlnumBench.php
<?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
<?php
/**
* Sample PHP script to check if a string is available to use in namespace.
*
* View this script in action online:
* https://paiza.io/projects/cjheAXgzbvk4uEq8civKAA
* View this script in Gist:
* https://gist.github.com/KEINOS/b72391409f201777f8c4420a59a9c282
*/
@KEINOS
KEINOS / LOG_Build-Error.sh-session
Created Jul 6, 2019
[LOG] Error log on heroku
View LOG_Build-Error.sh-session
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: 0.0.0.0:80 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: 0.0.0.0:80 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: 0.0.0.0:80 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: 0.0.0.0:80 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: 0.0.0.0:80 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: 0.0.0.0:80 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: 0.0.0.0:80 Permission denied
2019-07-06T16:11:44.101447+00:00 app[web.1]: 2019-07-07 01:11:44: (network.c.313) can'
@KEINOS
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"
}
View LOG_Build_CentOS_DockerImage.sh-session
$ # 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