programming

Convert a Slice to a Map in Go (Set, Index Map, Group Duplicates)

Build a map from a slice in Go: use elements as keys for set-like maps, pick struct fields as keys, map indices to values, or map keys to slices of positions for duplicates.

Deepak Prasad3 min read
programming

Convert int to string in Go (strconv, fmt, bytes.Buffer)

Convert int to string in Go with strconv.Itoa or strconv.FormatInt, fmt.Sprintf or fmt.Fprintf to an io.Writer, and strings.Builder for custom assembly; compare with general type conversion in Go.

Deepak Prasad2 min read
programming

Go context Package: Cancel, Timeout, Deadlines, and Values

Use golang context for deadlines and cancellation: Background and TODO roots, WithCancel, WithTimeout, WithDeadline, and WithValue for request-scoped data; always call cancel and pass ctx as the first parameter.

Deepak Prasad4 min read
programming

Golang map and map literal: basics, global maps, and iteration

Golang map and maps in golang: map[K]V, golang map literal and make, nil map panic, comma-ok lookup, delete and range, ordered iteration via sorted keys, golang global map and concurrency, dictionary analogy to Python.

Antony Shikubu5 min read
programming

Golang Shiny: windows, events, and drawing with x/exp/shiny

Experimental desktop GUIs with golang.org/x/exp/shiny: driver.Main, screen buffers, paint and lifecycle events from x/mobile, resize-safe drawing, and when to prefer Fyne, Gio, Ebiten, or Wails.

Deepak Prasad8 min read
programming

Stop goroutines in Go: cancellation, context, and clean shutdown

Cooperative goroutine shutdown in Go: no kill API, done channels, context cancel and timeouts, worker drains, pipeline-style leaks, WaitGroup, defer cleanup, and cheat sheet vs force kill.

Tuan Nguyen9 min read
programming

Split a string in Go: strings.Split, Fields, Cut, and SplitN

strings.Split and SplitN, Fields for whitespace, Cut for the first separator, cleaning empty tokens and TrimSpace, CSV caveats with encoding/csv, Split vs regexp.Split, and common mistakes.

Tuan Nguyen8 min read
programming

Go string interpolation: fmt.Sprintf, verbs, and alternatives

Go has no f-strings or ${} interpolation; use fmt.Sprintf and format verbs, Printf vs Sprintf vs Sprint, number padding and bases, struct verbs, Builder and text/template alternatives, and common fmt mistakes.

Tuan Nguyen7 min read
programming

Logrus for Go: golang logrus, levels, JSON, files, and hooks

Golang logrus and go logrus: install github.com/sirupsen/logrus, logrus log levels with SetLevel, TextFormatter and JSONFormatter, io.MultiWriter, SetReportCaller, hooks, and how long go.sum lines relate to the module …

Antony Shikubu5 min read
programming

Golang log to file, stdout, and syslog with the standard log package

Golang log to file with os.OpenFile and log.SetOutput; golang log to file and stdout using io.MultiWriter; golang log syslog on Linux; go import log and golang log example; concurrency-safe default log.Logger.

Tuan Nguyen4 min read
programming

Go functions with multiple types: `any`, type switches, and generics

Golang function parameter multiple types: use any/interface{} with a type switch, golang function types with type parameters (generics), go function types and golang func type constraints, plus golang pass type as …

Tuan Nguyen5 min read
programming

Call a Function From Another Package or File in Go

Call Go code from another file or another package: what to import, when you do not need an import, capital letters for public functions, and how to fix typical errors.

Deepak Prasad8 min read
programming

go list installed packages: list Go packages, modules, and install paths

go list command for std, ./..., and all; go list -m all for modules; where Go stores packages (GOMODCACHE, GOROOT, GOPATH/bin); list go packages and binaries from go install without mistaking modules for a global package …

Deepak Prasad5 min read
programming

Golang Anonymous Function Explained with Examples

Learn anonymous functions in Go: function literals, immediate calls, parameters and return values, variables, callbacks, closures, goroutines, and common mistakes like loop capture.

Deepak Prasad7 min read
programming

Golang os exit, defer, and why deferred functions are not run

Golang os exit defer: os.Exit golang behavior matches pkg.go.dev os Exit — program terminates immediately and deferred functions are not run; patterns to run cleanup before exit, go os.exit deferred functions official …

Tuan Nguyen5 min read
programming

Golang reverse range and iterate a slice backwards

Golang reverse range and golang range reverse with a backward index loop; golang range backwards using forward range and remapped index; golang iterate over slice and golang iterate slice backwards; golang reverse slice …

Tuan Nguyen4 min read
programming

Golang split string and assign to variables: strings.Split, Fields, and Cut

Golang split string and golang string split with strings.Split and strings.Fields; strings split golang and go split string; golang split string into two variables with strings.Cut or slice indexing; net.SplitHostPort …

Tuan Nguyen4 min read
programming

Golang set cookie and golang http cookie with net/http

Golang set cookie with http.SetCookie and http.Cookie, golang http cookie headers, golang cookies and cookie golang reads via Request.Cookie, Path Domain MaxAge Expires SameSite Secure HttpOnly, order before Write, curl …

Tuan Nguyen5 min read
programming

Golang iterate over array: range and for loops with len

Golang iterate over array and golang array loop with for range; golang for loop array and golang loop through array using len; go iterate over array and iterate over array golang with index or values only.

Tuan Nguyen4 min read
programming

Convert Go []byte to string (Casting, Builder, Buffer, strconv)

Convert go bytes to string with string(b), fmt.Sprintf, strings.Builder or bytes.Buffer, strconv for numbers, and know when copying happens for UTF-8 text and binary data.

Deepak Prasad3 min read
programming

Use Go as a Redis Client (go-redis, Ping, Get, Set, Hashes)

Connect a Go program to Redis with github.com/redis/go-redis/v9, Ping with context, Set and Get strings, and store hashes; run Redis locally and match password and DB settings.

Antony Shikubu3 min read
programming

Get Current Time in Milliseconds in Go (UnixMilli and Alternatives)

Convert time.Time to Unix milliseconds with UnixMilli, derive ms from UnixNano, or use Duration.Milliseconds after Sub; know int64 range limits from the time package docs.

Tuan Nguyen3 min read
programming

Golang gRPC tutorial: server, client, and Protocol Buffers example

Golang grpc example and grpc golang tutorial: proto3 service, golang grpc server and grpc server golang with Register and Serve, go grpc client and go grpc example with grpc.NewClient and transport credentials, grpc go …

Antony Shikubu5 min read
programming

Golang integer types: int, int32, int64, uint, and sizes

Golang int and go int types, golang int types and sizes, golang int vs int32, uint and sized integers, byte and rune aliases, and how golang data types size depends on int versus int8–int64.

Deepak Prasad5 min read
programming

Golang CPU affinity on Linux: sched_setaffinity, taskset, GOMAXPROCS

Golang CPU affinity on Linux: cpu masking with golang.org/x/sys/unix SchedSetaffinity, taskset from Go, runtime.LockOSThread for set thread affinity, and why gomaxprocs golang is not affinity.

Deepak Prasad4 min read
programming

FlashDB in Go: in-memory key-value store with optional persistence

FlashDB golang tutorial: an in memory database golang option and golang key value store with Redis-style types. Covers go in memory database setup, transactions, string SET/GET/DELETE, disk vs pure memory, and links for …

Tuan Nguyen5 min read
programming

Golang nil checks: pointers, interfaces, slices, and empty structs

Golang check if nil for pointers and maps; golang null pointer with == nil; check if slice is empty golang (len vs nil); golang check if struct is empty (not nil—reflect.IsZero); golang check if interface is nil …

Tuan Nguyen5 min read
programming

Go HTTPS Client: Skip TLS Verify (InsecureSkipVerify) and Safer Options

Use tls.Config InsecureSkipVerify on a dedicated net/http Transport for golang http client skip tls verify only in dev; prefer fixing golang ssl certificate trust with RootCAs instead of golang disable ssl verification …

Tuan Nguyen5 min read
programming

Golang flag package examples: CLI flags, multiple values, subcommands

Golang flag and golang flags examples using the standard library flag package: go flag and go CLI flags, golang flag example for string, int, and bool, golang flag multiple values with flag.Value, required flags, …

Antony Shikubu6 min read
programming

Golang seek on files: os.File.Seek and whence constants

Golang file seek and go file seek with (*os.File).Seek: io.SeekStart, io.SeekCurrent, io.SeekEnd, temp-file examples, and bytes.Reader for in-memory golang seek.

Tuan Nguyen4 min read