Skip to content

Instantly share code, notes, and snippets.

Avatar

KEINOS KEINOS

View GitHub Profile
@KEINOS
KEINOS / install_ruby_rpi.sh
Last active Jun 8, 2022 — forked from zzidante/install_ruby_rpi.sh
A Bash script to install Ruby 2.6.8 on the Raspberry Pi OS
View install_ruby_rpi.sh
#!/bin/bash
# --------------------------------------------------------------------------------------------
# Installs Ruby 2.6.8 using rbenv/ruby-build on the Raspberry Pi OS
#
# Run from the web:
# bash <(curl -sL https://gist.github.com/KEINOS/7101f542be23e5048198e2a27c3cfda8/raw/install_ruby_rpi.sh)
# --------------------------------------------------------------------------------------------
# Welcome message
@KEINOS
KEINOS / nurupo.go
Last active Apr 16, 2022
[Golang] Gist is a git. Thus, it can be used in "import" of Go. See in action: https://go.dev/play/p/3W-F9T113d_f @ Go Playground
View nurupo.go
// To see how to use this gist: https://go.dev/play/p/3W-F9T113d_f @ Go Playground
// Ref: https://qiita.com/mattn/items/6cad7b5dd73092b3d354 by @mattn @ Qiita
package nurupo
import (
"errors"
"strings"
)
// Nurupo returns an error if the message contains "ぬるぽ".
@KEINOS
KEINOS / convert-zero-padded-decimal-to-int.md
Last active Apr 18, 2022
[Golang] Zero padded decimal string to integer / Decimal string with leading zeros to integer
View convert-zero-padded-decimal-to-int.md

[Golang] How to convert a zero-padded decimal string to an integer?

Converting decimal string with leading zeros to integer

str "000123" --> int 123

To trim a zero-padded decimal string and convert it to integer as shown above, simply use strconv.Atoi() function.

@KEINOS
KEINOS / config.yaml
Last active Feb 26, 2022
K3OS config.yaml
View config.yaml
ssh_authorized_keys:
- "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCq44h2dipTqOlGog5RhqLXJE2ngfvdF2wRHF9aXxPGoh8VxdYAjXtGnuDXy/yhGGyzkSFjBZmseN/uFUkVA0Pa1mFtJUaFsYQo/AMrITQNai5DeKJoIga98xubAMpHgf6c9k0DyTo4R1VmLfrDAvcCU874n6j1coJxIpjCUSXA01SSSGoTqxTuZsB3imxraQE0nC1NvFdANAiA4HcmgYyMKfhvUev/letizKnWPVVUdG6uoUrSS4cZrTvfOAS0HQWo6cMKArn6FPnng2NPK9N6BNojpesfhVd4JiedW8jXHG93kjmaRnHkj/kjw2wl6Ud4fyDbA//mPEkHA3AhhktS9BkOf2Dpfa71phbFW4Yg/iwh6u9WpeLMgmSGA5jWg6puqNNdMr/PyNQwi34S6DX3c0eF3WMr8MHGyrSOQviu/YGaxuBFnFcn1w3QBQBxr7+MTf7bA4qeb53l2dFqx3i6YEjrXvPIBqQlk/iAVy1opkbmfRrWoJjgz84p5Om6muxN94xj2bywW03NUaafFJvRZYQ7zvKqzTq7NQqQAo1HLGy/vhZpRhYRqctBpzu3+bJlzFrqaSBmLjuLTDMTvHNPrORQoFPKzfa/SON3fHRmNjRaMpTLJDbaNAEaJXIHwnqpfLgelzrrKTJgH9BsABy2LTO9VX47UiPGatA4QIo/ZQ=="
hostname: k3os-server-keinos01
write_files:
- content: |
nameserver 8.8.8.8
nameserver 8.8.4.4
owner: root:root
path: /etc/resolv.conf
permissions: '0644'
@KEINOS
KEINOS / how-to-get-verion-module.md
Created Sep 27, 2021
[Golang] How to get imported module/package version from main.go
View how-to-get-verion-module.md

[Golang] How to get the version of imported non-standard module from main

tl; dr

Use debug.ReadBuildInfo() of "runtime/debug" module.

ts; dr

This is an example to get the version of "golang.org/x/xerrors" package from the main source code.

@KEINOS
KEINOS / how_to_mock_os.Stdin.md
Last active May 6, 2022
【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 and uses os.Stdin, 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 Aug 4, 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 Apr 1, 2022
[Golang][BadgerDB] How to suppress/hide error messages
View how-to-hide-info-msg-in-badger.md

[Golang] 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 Aug 12, 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'