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.

Channel

  • a Channel is a communication pipe that allows different Goroutines (concurrent threads) to talk to each other and synchronize their execution.

Commands

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

  • 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

  • After build , the binary executable(.exe) file will be created, run the file

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)

Last updated