Go Tips

Here lies my collection of random things I've learned how to do in Go.

Recurring Tasks

2025-04-28

package main

import "time"

func NewHourlyCron(f func()) {
    ticker := time.NewTicker(time.Hour)
    defer ticker.Stop()

    for {
        select {
        case <-ticker.C:
            f()
        }
    }
}

Format Time in Kitchen Time

2025-04-28

package main

import (
	"fmt"
	"time"
)

func Example() {
	fmt.Println(time.Now().Format(time.Kitchen))
	// Output:
	// 11:00PM
}

Capitalize Words

2025-04-28

package main

import (
	"fmt"

	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

func Capitalize(s string) string {
	return cases.Title(language.English).String(s)
}

func ExampleCapitalize() {
	fmt.Println(Capitalize("abcdef"))
	fmt.Println(Capitalize("hi there"))
	fmt.Println(Capitalize("CAPITALIZED"))

	// Output:
	// Abcdef
	// Hi There
	// Capitalized
}