Skip to content

Instantly share code, notes, and snippets.

@KentaKudo
KentaKudo / main.go
Created October 13, 2023 06:36
A Go script to generate caption file (srt format) from text file
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"time"
)
@KentaKudo
KentaKudo / ctx.go
Last active February 18, 2022 10:02
Go Best Practice
func (u *User) Store(ctx context.Context) error {
...
if err := u.Hash.Store(ctx, k, u); err != nil {
return err
}
...
}
@KentaKudo
KentaKudo / cc.Makefile
Last active March 8, 2019 16:09
A snippet for the Makefile of the C compiler on x86_64 in working with macOS
WORKDIR := /usr/src/mycc
mycc: mycc.c
@docker run --rm -v ${PWD}:${WORKDIR} -w ${WORKDIR} gcc:8.3 gcc -o mycc mycc.c
.PHONY: test
test: mycc
@docker run --rm -v ${PWD}:${WORKDIR} -w ${WORKDIR} gcc:8.3 ./test.sh
.PHONY: clean
@KentaKudo
KentaKudo / webapp.go
Last active February 14, 2019 14:21
A rough idea of a web app framework in Go
package webapp
import (
"fmt"
"net/http"
)
type Error struct {
code int
}
@KentaKudo
KentaKudo / task_queue.go
Last active February 3, 2019 15:55
A sample implementation of a task queue in go
// Inspired by Go in Action: Chapter 7 – Concurrency patterns
// https://www.manning.com/books/go-in-action
package main
import (
"fmt"
"os"
"sync"
)
@KentaKudo
KentaKudo / sheet.py
Created February 3, 2019 08:24
A sample code to access Google Sheet in Python
# See: https://developers.google.com/sheets/api/quickstart/python
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/spreadsheets'] # read/write access
SHEET_ID = '<sheet_id>'
@KentaKudo
KentaKudo / json_to_csv.py
Created February 2, 2019 22:33
An example to parse json into csv format in Python
import json # https://docs.python-guide.org/scenarios/json/
import csv # https://docs.python.org/3/library/csv.html
json_string = '[\
{"gender": "Male", "Nationality": "JPN", "document_type": "passport"}\
]'
parsed_json = json.loads(json_string)
with open('out.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for row in parsed_json:
@KentaKudo
KentaKudo / query.go
Created January 21, 2019 19:43
Go Code Snippet: URL.Query()
func handler(w http.ResponseWriter, r *http.Request) {
queries := r.URL.Query() // Values = map[string][]string
for key, values := range queries {
for _, v := range values {
fmt.Fprintf(w, "key: %s, value: %s\n", key, v)
}
}
}
@KentaKudo
KentaKudo / get.go
Created January 20, 2019 15:21
Go Code Snippet: http.Get
func (c *Client) Get(url string) (string, error) {
resp, err := c.cli.Get(url)
if err != nil {
return "", err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
@KentaKudo
KentaKudo / httptest_test.go
Last active August 10, 2022 15:52
Table Driven Testing example of httptest
func TestHeader(t *testing.T) {
cases := []struct {
input []string
want map[string][]string
}{
{
input: []string{"X-Test: test"},
want: map[string][]string{"X-Test": []string{"test"}},
},
{