- Writing Idiomatic Go
- Β© 2026 GoCloudStudio
- Patterns, Practices, and Hard-Won Lessons for Production Go
- GoCloudStudio Engineering Team
- February 2026 β’ Version 1.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Controller, Get, Req, Res, UseGuards } from '@nestjs/common'; | |
| import { Request, Response } from 'express'; | |
| import { GoogleOauthGuard } from './google-oauth.guard'; | |
| @Controller('auth/google') | |
| export class GoogleOauthController { | |
| constructor(private jwtAuthService: JwtAuthService) {} | |
| @Get() | |
| @UseGuards(GoogleOauthGuard) |
π Minimize allocations in Go
Working with protobuf in Go puts significant load on the memory subsystem, as protobuf-generated structures often contain a significant amount of pointers.
One way to minimize the number of allocations is to allocate all the fields at the same time, and then use internal pointers to wire them up.
import (
// ...
"github.com/rs/cors"
)
// ...
mux := runtime.NewServeMux()
proto.RegisterXYZServiceHandler(ctx, mux, conn)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "net/http" | |
| "time" | |
| "golang.org/x/time/rate" | |
| ) | |
| // ThrottledTransport Rate Limited HTTP Client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function slice() { | |
| local slice_width="${SLICEW:-1080}" | |
| local width=$(identify -format "%[w]" $1) | |
| local slices=$(($width/$slice_width)) | |
| local name="${1%.*}" | |
| echo -e "slice ($slices x $slice_width) for image $1" | |
| convert -crop "${slices}x1@" $1 "${name}-%d.png" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Note, this file is written by cloud-init on first boot of an instance | |
| ## modifications made here will not survive a re-bundle. | |
| ## if you wish to make changes you can: | |
| ## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg | |
| ## or do the same in user-data | |
| ## b.) add sources in /etc/apt/sources.list.d | |
| ## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl | |
| # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to | |
| # newer versions of the distribution. |
# https://stackoverflow.com/a/12019115/2272992 by Awk
^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$
βββββββ¬ββββββββββ¬ββββββββββ¬βββββββββββββ¬ββββββ βββββ¬ββββ
β β β β no _ or . at the end
β β β β
β β β allowed characters
β β β
β β no __ or _. or ._ or .. insideNewerOlder
