Golang

Pros & Cons

Pros

  • High performance: Compiled to fast, standalone native binaries with efficient memory usage.

  • Concurrency: Built-in lightweight goroutines enable high concurrency and multi-core parallelism.

  • Garbage collection: Efficient, low-latency GC designed for server workloads.

Cons

  • Slower initial development: More verbose syntax and static typing require more upfront coding.

  • Smaller ecosystem: Compared to Node.js, fewer third-party libraries and tools.

  • Learning curve: Developers must learn new paradigms like goroutines and static typing.

Goroutines

  • Goroutines have a small initial stack size (typically around 2-4 kilobytes) mainly to be lightweight and efficient in terms of memory usage. This design contrasts with traditional OS threads like Java threads, which usually have much larger fixed stack sizes (often 1-2 MB).

Reasons for small goroutine stacks:

  • Memory Efficiency: Go programs often spawn thousands to millions of goroutines. If each had a large fixed stack, total memory usage would be enormous. The small initial stack lets many goroutines coexist without large memory overhead.

  • Dynamic Stack Growth and Shrinking: The Go runtime dynamically grows and shrinks each goroutine’s stack as needed. When a goroutine runs out of stack space, the runtime allocates a larger stack, copies the data, and continues execution, enabling safe, flexible stack size management.

  • Fast Startup: Small initial stacks mean goroutines start with minimal overhead and quickly, improving concurrency throughput.

  • Avoiding Over-Provisioning: Instead of guessing a large stack size upfront, Go allocates only what a goroutine needs, reducing wasted memory.

Commands

  • The command used to initialize modules and manage dependencies is:


go mod init <YOUR_MODULE_NAME> (to create a new module with a go.mod file) (similar with npm init)
go mod tidy (to add missing and remove unused dependencies) (similar with npm i)
go get <DEPENDENCY> (to add/update specific dependencies) (similar with npm i xxx)
  • The primary file for managing dependencies is the go.mod file. It lists all the module dependencies and their versions that your project requires.

  • The go.sum file complements go.mod by storing cryptographic checksums of the exact dependency versions to ensure integrity and reproducibility in builds.

Here is the command of local run and build

## local run
APP_ENV=local APP_PORT=5000 go run ./cmd/internal-api/main.go
## local build
go mod tidy
go build -o hidmember ./cmd/frontend-api/ && APP_ENV=local APP_PORT=5008 ./hidmember

Example workflow:

  • Run go mod init to create a go.mod file.

  • Use go get to add a dependency, which updates go.mod.

  • Use go mod tidy to ensure go.mod matches source imports and cleans unused dependencies.

  • Both go.mod and go.sum should be committed to version control.

Example of go.mod (similar with nodejs package.json)

module hld/hldid-member

go 1.24.0

toolchain go1.24.4

replace hld/common => ../common

require (
	github.com/gin-contrib/cors v1.7.6
	//...
)

require (
	github.com/aws/aws-sdk-go-v2 v1.39.3 // indirect
	//...
)

require (
	filippo.io/edwards25519 v1.1.0 // indirect
	//...
)

Last updated

Was this helpful?