1. Home
  2. /
  3. Go
  4. /
  5. Counter with Goroutines

Counter with Goroutines - Go Typing CST Test

Loading…

Counter with Goroutines — Go Code

Demonstrates a simple counter updated concurrently with goroutines.

package main

import (
	"fmt"
	"sync"
)

func main() {
	var count int
	var mu sync.Mutex
	var wg sync.WaitGroup

	for i := 0; i < 5; i++ {
		wg.Add(1)
		go func() {
		defer wg.Done()
		mu.Lock()
		count++
		fmt.Println("Count:", count)
		mu.Unlock()
		}()
	}

	wg.Wait()
}

Go Language Guide

Go (Golang) is a statically typed, compiled programming language designed at Google. It emphasizes simplicity, concurrency, and high-performance networking and system programming, making it ideal for cloud services, web backends, and distributed systems.

Primary Use Cases

  • ▸Backend web services and APIs
  • ▸Cloud-native and distributed systems
  • ▸Command-line utilities
  • ▸Network programming and microservices
  • ▸DevOps and infrastructure tooling

Notable Features

  • ▸Compiled and statically typed with fast binaries
  • ▸Goroutines and channels for concurrency
  • ▸Automatic memory management via garbage collection
  • ▸Powerful standard library, especially for networking and web
  • ▸Cross-platform compilation support

Origin & Creator

Created by Robert Griesemer, Rob Pike, and Ken Thompson at Google in 2007 and publicly released in 2009.

Industrial Note

Go is specialized for backend systems, cloud infrastructure, networking, and concurrent applications rather than heavy GUI or mobile frontend development.

Quick Explain

  • ▸Go has a simple syntax, garbage collection, and built-in support for concurrent programming via goroutines and channels.
  • ▸It produces fast, statically linked binaries and supports cross-compilation across platforms.
  • ▸The Go standard library is extensive, especially for networking, HTTP, and system-level tasks.

Core Features

  • ▸Simple, clear syntax for easy readability
  • ▸Goroutines for lightweight concurrent execution
  • ▸Channels for communication and synchronization
  • ▸Interfaces for polymorphism
  • ▸Package-based modular system

Learning Path

  • ▸Learn Go syntax and variables
  • ▸Understand structs, interfaces, and methods
  • ▸Practice goroutines and channels
  • ▸Build CLI or HTTP applications
  • ▸Contribute to Go open-source projects

Practical Examples

  • ▸HTTP REST API server using `net/http`
  • ▸Concurrent file processing with goroutines
  • ▸Network socket communication
  • ▸CLI tool for system monitoring
  • ▸JSON parsing and data transformation

Comparisons

  • ▸Simpler syntax than C++ or Java
  • ▸Faster than interpreted languages like Python
  • ▸Built-in concurrency unlike many languages
  • ▸Statically compiled like Rust or C
  • ▸Ideal for networked services and cloud applications

Strengths

  • ▸High performance due to compilation
  • ▸Concurrency primitives built-in and easy to use
  • ▸Strong standard library for common tasks
  • ▸Cross-platform compilation
  • ▸Easy deployment as a single statically linked binary

Limitations

  • ▸No generics before Go 1.18 (now available but limited)
  • ▸Minimalist standard library for GUI or graphics
  • ▸Error handling requires explicit checks
  • ▸Limited metaprogramming or macros
  • ▸Lacks some modern language features like operator overloading

When NOT to Use

  • ▸Desktop GUI-heavy applications
  • ▸Mobile app frontend (though supported via gomobile)
  • ▸Real-time high-performance graphics/games
  • ▸Heavy metaprogramming or DSLs
  • ▸Scripts for text processing with minimal compilation overhead

Cheat Sheet

  • ▸var x int - declare variable
  • ▸x := 42 - short variable declaration
  • ▸func add(a, b int) int { return a+b } - define function
  • ▸go f() - start goroutine
  • ▸ch := make(chan int) - create channel

FAQ

  • ▸Is Go suitable for web development?
  • ▸Yes, Go is widely used for web servers and APIs.
  • ▸Can Go handle concurrent tasks?
  • ▸Yes, goroutines and channels make concurrency simple.
  • ▸Which platforms support Go?
  • ▸Windows, Linux, macOS, BSD, ARM, and others.
  • ▸Is Go statically or dynamically typed?
  • ▸Go is statically typed.
  • ▸Does Go support object-oriented programming?
  • ▸Yes, via structs and interfaces, though without classes.

30-Day Skill Plan

  • ▸Week 1: Go basics, variables, control flow
  • ▸Week 2: Functions, structs, and methods
  • ▸Week 3: Concurrency and channels
  • ▸Week 4: Networking and HTTP servers
  • ▸Week 5: Testing, profiling, and deployment

Final Summary

  • ▸Go is a compiled, statically typed language designed for simplicity, performance, and concurrency.
  • ▸It excels at backend services, cloud applications, and distributed systems.
  • ▸Goroutines and channels provide easy concurrency.
  • ▸Go produces fast, portable binaries with minimal dependencies.
  • ▸Strong standard library and tooling make it highly productive for system programming.

Project Structure

  • ▸cmd/ - main applications
  • ▸pkg/ - libraries or reusable packages
  • ▸internal/ - private modules
  • ▸api/ - API definitions or proto files
  • ▸test/ - additional test cases

Monetization

  • ▸Develop cloud infrastructure tools
  • ▸SaaS backend services
  • ▸CLI utilities for enterprises
  • ▸Open-source consulting and support
  • ▸Educational courses for Go developers

Productivity Tips

  • ▸Use `go fmt` and `go vet` regularly
  • ▸Leverage standard library extensively
  • ▸Use goroutines and channels for concurrency
  • ▸Keep code modular and reusable
  • ▸Automate testing and deployment with CI/CD

Basic Concepts

  • ▸Variables with `var` or short declaration `:=`
  • ▸Functions and methods
  • ▸Structs and interfaces
  • ▸Control flow: `if`, `for`, `switch`
  • ▸Concurrency with `go` and channels

Official Docs

  • ▸https://golang.org/doc/
  • ▸Go standard library documentation
  • ▸Go blog and tutorials

More Go Typing Exercises

Go Theme ToggleGo Score TrackerGo Simple TimerGo Health TrackerGo Level TrackerGo Coin CounterGo Ammo TrackerGo Star Collector

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher